Daemon
Daemon

Reputation: 65

Compare Images in Pygame(Not Pixel by Pixel)

I have an array of images in which there are multiple images , say for example,

guns = [pygame.image.load('gun1.jpg'),pygame.image.load('gun2.jpg'),pygame.image.load('gun3.jpg'),pygame.image.load('gun4.jpg')]

These all images are yellow rectangles and I have changed some of them to white rectangles of same dimensions to make them disappear in my code.

Now I have to check that in that array of guns which images are white and which are not.

For that I tried looping:

for i in range(0,window_size):
    print('wolla2!')
    if guns[i] is whiteIm:
        print('wolla!')
    i+=1

where whiteIm is the white colored image I replaced with and window_size is the number of images. But I know I can't do that like this.

How to check that the image is white or not in the array?

Upvotes: 1

Views: 435

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36722

Here are Four plus One possibilities for you to evaluate, and choose from, based on your circumstances:

Subclass Image:

you could maybe subclass the Image class to add an attribute?

Dynamically add an attribute to Image:

in Python, you can add an attribute dynamically: gun1.color = "w"

Maintain a parallel array:

the images are stored in an array, it might be easy to maintain an array of booleans in parallel, where False is yellow, and True is White for instance.

prefix the name of the image file:

As you are naming these files after "whitening" them, you could add a w_ prefix, and then segregate based on their name? gun1.jpg --> w_gun1.jpg
These images could then be segregated based on the first characters of their __name__

Add a tag: (maybe?)

in tkinter, you can add tags to canvas objects, and manipulate them as a group based on their tags... maybe pygame offers a similar possibility?

Upvotes: 2

Related Questions