Check if pictures are similar in Python - python

I'm facing the following problem:
I have N pictures (300x300pixels, anchor is always top left). Of these N pictures, some are are the same, just mirrored and/or rotated.
As you see, the first 2 parts are nearly the same, just mirrored, while part 3 is completely different.
def compare_Image(sourceIMGPath, compareIMGPath):
sourceIMG = Image.open(sourceIMGPath, "r")
compareIMG = Image.open(compareIMGPath, "r")
for rotation in [0, 1]:
if rotation == 1: compareIMG = ImageOps.mirror(compareIMG)
for angle in [0, 90, 180, 270]:
compareIMG = compareIMG.rotate(angle)
imgDiff =ImageChops.difference(sourceIMG, compareIMG)
if not imgDiff.getbbox():
compareIMG.show()
sourceIMG.show()
print(str(Path(sourceIMGPath).stem) + " " + str(Path(compareIMGPath).stem))
return True
return False
I tried to mirror and rotate the picture, but surely this doesn't work, since the picture will move to the side/bottom of the picture which will move the anchor and so the picture will never be the same.
So I need to find a way, in which I maybe can reset the anchor or something similar. Maybe someone has an idea.

Related

3 lists keep becoming copies of each other even when only one list is changed

Context: I'm a low intermediate level programmer trying to make a game with my own code in order to improve my ability. I know pygame etc... exists but I wanted to try and make my own engine. I'm currently trying to create a display system. My thought process was to create a 2D list: SURFACE where SURFACE[y][x] is a 'pixel' at coordinates x,y on the python console. This worked great. But I wanted more, and thought: Why don't I create 3 levels of surface, BACKSURFACE, MIDSURFACE, and FRONTSURFACE. My idea was that I could create a single flattened image by combining whatever is rendered through BACKSURFACE, MIDSURFACE, and FRONTSURFACE. This way I could do things like, create an options menu that overlays on top of the game without clearing the entire display.
Now comes my Issue. I created a function addToSurface() that takes a surface, the x, y positions of where to render the image/text, and a list that contains the render item.
def addToSurface(surface, x_pos, y_pos, renderItem):
global SCREENWIDTH, DISPLAYHEIGHT
returnSurface = []
returnSurface = surface.copy() # Make a copy of the surface list
itemHeight = len(renderItem)
itemWidth = len(renderItem[0])
if itemHeight <= DISPLAYHEIGHT and itemWidth <= SCREENWIDTH:
render_y = 0
for y in range(y_pos, itemHeight+y_pos):
render_x = 0
for x in range(x_pos, itemWidth+x_pos):
if renderItem[render_y][render_x] == " ": # Because the 'image' files are ASCII, I turn every
# space in the image into a [None] pixel. The
# original surface lists are initialised where
# every pixel is [None]
returnSurface[y][x] = [None]
else:
returnSurface[y][x] = renderItem[render_y][render_x] # If a pixel in the image isn't empty
# the equivalent pixel coordinate in the
# surface becomes the pixel.
render_x += 1
render_y += 1
return returnSurface # A new version of the surface is returned.
For example, if I run BACKSURFACE = addToSurface(BACKSURFACE, 0, 3, logoImage) where logoImage is a 2D list containing an ASCII image of my games logo, what I expect is for BACKSURFACE to now contain the logo at starting at BACKSURFACE[3][0] (coordinates 0, 3). Like I mentioned above, this works great. But for some reason, after creating my MIDSURFACE, and FRONTSURFACE lists, everytime I run addToSurface() all three lists are changed.
So now, when I run BACKSURFACE = (addToSurface(BACKSURFACE, 0, 3, logoImage)), not only does BACKSURFACE come with the logo rendered, so do MIDSURFACE and FRONTSURFACE, even though they haven't been referenced in the program at all after initialisation. For reference, here are my other functions in the program so far:
def __init__(height, width):
global SCREENWIDTH, SCREENHEIGHT, TITLE, BACKSURFACE
global MIDSURFACE, FRONTSURFACE, DISPLAYHEIGHT
SCREENWIDTH = width
SCREENHEIGHT = height
DISPLAYHEIGHT = height - 11 #The above 3 lines are just to set display size.
for i in range(DISPLAYHEIGHT): # This loop creates the 2D lists for all surfaces
temp = [] # where each pixel has a default value of [None]
BACKSURFACE.append(temp)
MIDSURFACE.append(temp)
FRONTSURFACE.append(temp)
for j in range(width):
temp.append([None])
def openImage(imageName, centre=False):
temp = []
dir = os.path.join(imageDirectory,imageName)
if not os.path.exists(dir):
return null
elif not centre:
with open(os.path.join(imageDirectory, imageName), "r") as f:
for line in f:
temp.append([char for char in line.rstrip("\n")])
f.close()
elif centre:
with open(os.path.join(imageDirectory, imageName), "r") as f:
for line in f:
centredLine = tech.centre(line.rstrip("\n"))
temp.append([char for char in centredLine])
f.close()
return temp
Like I mentioned above, I would describe myself as just past begginer level programming and this is definitely the most complex thing I've ever attempted. Even so, I have a feeling it's going to be something really simple. If you could point out where the error is and why it's an issue I'd be more than happy. If anyone could suggest a fix I'd appreciate it as well.

how would i check all adjacent cells for my minesweeper game?

I have been trying to figure out a way to check each adjacent cell for my minesweeper game and am coming up short. I am a beginner in python and would also like to start using OOP. however before I can even get there, I need to rectify this. all the tutorials I have seen don't use basic python, but different versions to the IDLE i use, so I am struggling. can anyone help me? I need to be able to go around each adjacent cell and check if there is a bomb there. The value to check if there is a bomb there is 1 and will also turn red. Thank you all so much!
also if you could dumb it down a little for me, that would be lovely.
import random
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WIDTH = 20
HEIGHT = 20
MARGIN = 5
bombnum = 10
grid = []
for row in range(10):
grid.append([])
for column in range(10):
grid[row].append(0)
print(grid)
pygame.init()
WINDOW_SIZE = [255, 315]
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Array Backed Grid")
done = False
clock = pygame.time.Clock()
#class bomb:
#def revealed(self,pick):#this is the grid thats been picked
# self.shown = shown
def placebomb():
for i in range(bombnum):
while True:
row = random.randint(0,8)
column = random.randint(0,8)
if grid[row][column] == 0:
grid[row][column] = 1
break
placebomb()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
column = pos[0] // (WIDTH + MARGIN)
row = (pos[1]-50) // (HEIGHT + MARGIN)
grid[row][column] = 1
print("Click ", pos, "Grid coordinates: ", row, column)
screen.fill(BLACK)
for row in range(10):
for column in range(10):
color = WHITE
if grid[row][column] == 1:
color = RED
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * (column) + MARGIN,
50+(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
clock.tick(60)
pygame.display.flip()
pygame.quit()
First up, you definitely want to make an OOP project out of this. Minesweeper's probably near the complexity limit of what you can reasonably do without object-oriented programming, but if you want to take the basic Minesweeper concept and make it more interesting / complex, you're going to need better structure.
And even if you're not considering making it more complex, thinking about what sorts of complexities you could add in is helpful in planning your classes. (Perhaps you didn't realize there's a "planning your classes" step? I'll get to that.) My steps here should work for pretty much any beginning OOP project, since it seems Minesweeper's just a convenient example.
I realize this answer will be a giant diversion from OP's how-do-I-check-the-nearest-neighbors question, but OP was also asking about OOP and answering their question in an OOP context means getting a class model in place. Trying to retrofit OOP onto non-OOP code is possible, but is usually harder than doing OOP from scratch.
If you're new to OOP in Python, check these two tutorials. Both of them run through the basics, neither requires an IDE, and neither introduces complexity / features beyond what you need to start. The rest of my answer will assume you're familiar with how to write classes. I highly recommend typing out both tutorials on your own terminal and trying a couple variants on them before you go further.
Now that you know roughly what a class is, make a list of all the operations you're going to want in your Minesweeper program, and what sorts of things come up in it. The point here isn't to write code, and natural language will serve as pseudocode here. Things you'll probably want to include: "Add a mine to a cell on the grid," "check a cell on the grid," "display the number of spots near a given cell on the grid." You might also include some "future plans" like "resize the grid in the middle of a game", "limit number of moves," "get hint," "allow mines to move," "have people that move through minefield," multiple types of checks / mines / cells, hex grid as an alternative to a square grid, strangely-shaped and 3D grids, etc. Don't worry if you don't know how to code those things yet. If you've got an interesting idea, include it on the list.
Go through that list and make some notes about the main words / concepts that keep coming up. These will be your classes. For Minesweeper, I'd list "Cell," "Grid," "Game," "Check," and "Mine," but your mileage may vary. These are not necessarily the final class list. They're just a step toward organizing your thoughts so you can turn the high-level "Minesweeper game" into something concrete & extensible. You can add / remove classes later once you get a better feel for which ones you'll actually use.
Note the relationships between objects of the classes, including which ones need to be described for the others to make sense / be instantiated. This step is analogous to how you might plan a relational database: A Game has one and only one Grid, a Grid is a collection of Cells arranged in a fixed pattern, a Cell may or may not contain a single Mine, a Check reveals whether a Mine is in a given Cell and if not, reveals the number of Mines in Cells adjacent to the Checked Cell, etc.
Open up your IDE, and start blocking out classes in a file. The point here isn't so much to write code as to get all your classes and notes into the code for easy reference. If this file starts to get big, think about how the classes group together and split that one file into several, with import statements connecting them. By "blocking out classes," I mean "writing the simplest classes which will compile, but don't try to make them run." At this stage, you can / should have classes that look like this:
class Grid:
"""A Game has one and only one Grid, a Grid is a collection of Cells arranged in a fixed pattern"""
def __init__(self, game, **setup_params):
"""Initialize the grid given a game and possibly other parameters for shape and size"""
raise NotImplementedError('This code has not yet been written.')
def resize_grid(self, new_size):
raise NotImplementedError('This code has not yet been written.')
def check_cell_at(self, position):
raise NotImplementedError('This code has not yet been written.')
Things to check: This is completely legal Python and compiles fine. All of your notes from steps 2-4 should end up in docstrings. All of the target functionality you described in your notes corresponds to particular methods on classes that have something to do with those functions. Every class you've described is present, and has a docstring describing its purpose & structure. Every class's __init__() method takes the arguments that are necessary to instantiate the class. Every method takes some parameters that will likely be helpful. You can always edit the parameter lists later, but again, the point is to organize your code before you get too far into writing it, so that relationships and functionality are easy to track. If you're using Git or another version-tracking tool, make your first commit as soon as you're done with this step.
Now that you've got all the structure blocked out, figure out what the "main entry point" is going to be, instantiate that class (probably Game in my approach), and check that your code compiles, runs, and exits with a NotImplementedError coming with the line and message you expect. (If so, that's success!) If the project feels big, or if you're going to be sharing code with other developers, you may also want to add unit tests at this stage. The expectation is that every test will fail, but writing the tests helps to document the planned functionality.
Fill in the methods one by one, add unit tests to go with new or updated methods, and try running the code a bit at a time. You don't have to write the methods in any particular order, but it will be easier to test once all the __init__() methods are complete, and those are generally straightforward.
Now, how do you check the neighbors of a cell?
You should have methods on a Cell for "get list of my neighbors," and "get whether this cell contains a mine." You probably also have a method on Grid to "get Cell at position." You might have multiple types of Checks, but the base Minesweeper game only has the one, so Cell has a method like
def check(self):
"""Returns "BOMB" if this cell has a Bomb. Otherwise, returns the number of neighbors with bombs as an integer."""
if self.has_bomb:
return "BOMB"
neighboring_mines = 0
for cell in self.grid.get_neighbors_of(self.position):
if cell.has_bomb:
neighboring_mines += 1
return neighboring_mines
There are only 8([1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,1],[-1,0],[-1,-1] relative to the clicked grid coordinate) adjacent squares, so it should be easy to do what you are asking only with "if" statements
anyway, just check the surrounding squares and if true add to a variable.
import random
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WIDTH = 20
HEIGHT = 20
MARGIN = 5
bombnum = 10
grid = []
for row in range(10):
grid.append([])
for column in range(10):
grid[row].append(0)
#print(grid)
pygame.init()
WINDOW_SIZE = [255, 315]
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Array Backed Grid")
done = False
clock = pygame.time.Clock()
#class bomb:
#def revealed(self,pick):#this is the grid thats been picked
# self.shown = shown
def placebomb():
for i in range(bombnum):
while True:
row = random.randint(0,8)
column = random.randint(0,8)
if grid[row][column] == 0:
grid[row][column] = 1
break
placebomb()
print(grid)
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
column = pos[0] // (WIDTH + MARGIN)
row = (pos[1]-50) // (HEIGHT + MARGIN)
grid[row][column] = 1
print("Click ", pos, "Grid coordinates: ", row, column)
NBombs = 0
for i in range(-1,2):
for k in range(-1,2):
if (i!=0 or k!=0) and (row+i>=0) and (column+k>=0) and (row+i<len(grid)) and (column+k<len(grid[0])):#prevents from both being 0, or for the index to be negative or out of range
print(i,k)
if grid[row+i][column+k] == 1:
NBombs+=1
print("Number of bombs:",NBombs)
screen.fill(BLACK)
for row in range(10):
for column in range(10):
color = WHITE
if grid[row][column] == 1:
color = RED
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * (column) + MARGIN,
50+(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
clock.tick(60)
pygame.display.flip()
pygame.quit()
So basically like another answer in this thread, you can check the surrounding/adjacent tiles by adding -1, 0, or 1 to both your current x and y.
You could write a separate function that would return a list of the coordinates of the surrounding tiles with bombs and take the tile coordinates and grid as arguments like the following:
def check_surrounding_tiles(current_x, current_y, grid):
bomb_tiles = []
#Check starting from position (current_x - 1, current_y - 1)
for i in range(-1, 2):
for j in range(-1, 2):
#Ignore the current x and y, and do bounds checking
if (i != 0 or j != 0) and (current_x + i) > -1 and (current_y + j) > -1 and (current_x + i) < len(grid) and (current_y + j) < len(grid[0]):
if grid[current_x + i][current_y + j] == 1
bomb_tiles.append[(current_x + i, current_y + j)]
return bomb_tiles
This, like mentioned above, will return a list of coordinate pairs with bombs in them.
If you need to get the number of bombs, simply use the following:
adjacent_bomb_count = len(check_surrounding_tiles(<x position>,<y position>, grid))

How do i add extra dimensions to a screenshot?

I currently have the code below.. and it takes the screenshot just fine.. but it cuts off the left and right side of the screenshot
How do i add another 10 pixels all around possible?
print "Taking SS..."
focusWindow = App.focusedWindow()
regionINFO = capture(App.focusedWindow())
shutil.move(regionINFO, os.path.join(r'C:\Screenshots', 'Dummy1.png'))
print "SS Done..."
It looks like you're taking a screenshot of a specific app. Try the whole screen.
import shutil # once at beginning of script
img = capture(SCREEN)
shutil.move(img, 'path')
The answer is present within your code itself. :)
focusWindow = App.focusedWindow()
Here, focusWindow stores the region of the App which the focus is upon. Either like Eugene has said above, you can go ahead to take the whole screenshot or you can append to the current of focusWindow and capture the region.
For eg.: Say you want to add 10 pixels on all sides, it would be like:
focusWindow.x -= 10
focusWindow.y -= 10
focusWindow.w += 20
focusWindow.h += 20
regionINFO = capture(focusWindow)
shutil.move(regionINFO, os.path.join(r'C:\Screenshots', 'Dummy1.png'))
print "SS Done..."

Scrolling objects are jerky and sometimes jump to the right a few pixels [pygame]

To create an abstract, scrolling city skyline for a prototype, I created a class that generates random rectangles. These rects are added to a list and items are pulled from that list to be drawn on the screen. The rects begin off screen to the right and scroll across to the left until they leave the view plane and are trashed. The movement of the buildings is oddly jerky and they also shift to the right a few pixels at a specific point on the screen.
This video of the prototype is fairly accurate with very little video capture lag. Pay attention to the gaps between the buildings, as they get within the right most 3rd of the display area, the gap will suddenly shrink as if the building to the left of the gap is suddenly shifting right a few pixels. The smaller the gap, the more noticeable it is. The other anomalies in the video are from the recorder and are not present in the app. Here's a very short video that clearly shows this phenomenon: https://www.youtube.com/watch?v=0cdhrezjcY8
At about 1 second in you'll notice a very narrow gap between buildings in the rear layer. At :04 seconds in the gap is even with the blue player object, the left rectangle shifts and the gap vanishes. There's a second, larger gap to the right of that one that does the same thing but since the gap is larger it doesn't completely vanish. I've looked over the code numerous times but can't see what could be causing this anomaly. I'm hoping someone can tell me if it's something I did or a limitation I'm encountering.
I originally wrote this program linearly, without classes or functions. I rewrote it using a class that generates layer objects and handles all the generating and scrolling. In both cases the problem exists. It's driving me crazy trying to figure out why the buildings do not move smoothly. I've even written a version of this using png images instead of randomly generated rectangles. In that version the pngs scroll smoothly and seamlessly: https://www.youtube.com/watch?v=Uiw_giAvbOo (The video is a bit jerky, but the actual program plays smooth) So the issue is limited to these random rectangles.
Here's the code for the program: https://www.refheap.com/73079
Here's the class code by itself:
class Scroller():
def __init__(self, speed, color, heightMax):
# Speed of the layer scroll, the color of the layer and the maximum height for buildings
# set up the building parameters
self.buildingHeightMax = heightMax
self.buildingHeightMin = 100
self.buildingWidthMax = 125
self.buildingWidthMin = 75
self.buildings = []
self.layerspeed = speed
self.buildTime = True
self.buildCountdown = 10
self.color = color
def update(self):
# Check if it's time to build. If not, decrement counter
if self.buildTime == False:
self.buildCountdown -= 1
# If time is 0, time to build, reset counter to a new random time
if self.buildCountdown <= 0:
self.buildTime = True
self.buildCountdown = random.randint(3, self.layerspeed)
# create building if it's time
if self.buildTime:
# generate random width and height of building
buildingHeight = random.randint(self.buildingHeightMin, self.buildingHeightMax)
buildingWidth = random.randint(self.buildingWidthMin, self.buildingWidthMax)
buildingTop = WINDOWHEIGHT - buildingHeight
# This generates the building object from the above parameters
building = pygame.Rect(WINDOWWIDTH, buildingTop, buildingWidth, WINDOWHEIGHT)
self.buildTime = False
self.buildCountdown = random.randint(3, self.layerspeed * 5)
# add building to buildings list
self.buildings.append(building)
# move all buildings on layer at set speed
for building in self.buildings:
# if the building is off the screen, trash it. If not, move it to the
# right at the objects speed.
if building.right < 0:
self.buildings.remove(building)
else:
building.left -= self.layerspeed
# draw the Front buildings
for i in range(len(self.buildings)):
pygame.draw.rect(windowSurface, self.color, self.buildings[i])
Your problem most likely lies in:
# move all buildings on layer at set speed
for building in self.buildings:
# if the building is off the screen, trash it. If not, move it to the
# right at the objects speed.
if building.right < 0:
self.buildings.remove(building)
else:
building.left -= self.layerspeed
You're using remove on the same list you're iterating from, and this will make it skip the next building. So it's not the building to the right that's moving faster, it's the one to the left that has skipped moving.
You can see it yourself with this simple example:
a = [2, 3, 4, 1.5, 6, 8, 3.2]
for element in a:
if element == 4:
a.remove(element)
else:
print element
Try it and you'll see that not only 4 won't be printed, but also 1.5 will be skipped.
Possibly a good way to do it is to first iterate through all the buildings to see which ones need to be removed, then remove then all, and finally move all the ones that are left.
You might want to check this link for some good suggestions.
You're also updating the countdown twice, first on line 47 and then on line 58. Is there any reason for this?

How to changed colours of some pixels of an image?

I'm trying to leave one third of the image stock, change all the black to yellow in the middle, and change the bottom third black to blue. I know how to change the colours, the problem I'm facing is I'm unaware of how I can select only one third of the pixels to manipulate them. Here s what I have..
def changeSpots1():
file = pickAFile()
picture = makePicture(file)
show(picture)
pix = getAllPixels(picture)
for p in pix:
intensity = (getRed(p) + getGreen(p) + getBlue(p))
c = getColor(p)
if (intensity < 150):
newColour = setColor(p, yellow)
repaint(picture)
I am using a program called JES to write this, incase you're wondering about commands like pickAFile.
Thank you for any help!
I know nothing about JES, but I'm going to guess that getAllPixels returns the pixels in the usual order: the first row, then the next row, then the next, etc.
If so:
pix = getAllPixels(picture)
third = len(pix) // 3
for p in pix[:third]:
# do top-third stuff
for p in pix[third:third*2]:
# do middle-third stuff
for p in pix[third*2:]:
# do bottom-third stuff
This does assume that the picture s divisible perfectly into thirds. If it's not, you will need to know the picture's width so you can round to the nearest complete row (because otherwise the top third might actually be 250 complete rows and the first 47 pixels of the 251st, which won't look very good). I don't know what function JES has to get the width, but I'm sure it's simple.

Categories