lundi 26 août 2019

If statement always returning false while trying to detect the color of a pixel

So I'm restarting working on a project with pygame, and I'm trying to make the background of a 50x50 image transparent (originally colored black), but I'm unable to change the color of the pixel

Here is the code I have now:

image2BCleared = Image.open("C:/Users/UserName/Desktop/PythonGame/image_player.png")
imageArray = numpy.array(image2BCleared)
width, height = image2BCleared.size
transparent = (0, 0, 0, 0)
newDataItem = (0,0,0,0)

for i in range(0, width):
    for j in range(0, height):
        newDataItem = imageArray[i][j]
        if newDataItem.all == (0, 0, 0, 255):
            imageArray[i][j] = transparent
            print(imageArray[i][j])
dirpath = os.getcwd()
im = Image.fromarray(imageArray)
im.save("img2.png", "PNG")

the when I run the program, there is no output from the print statement.

For some reason, it always goes to the "else" statement event though the content on the array is the same (I used this code):

imageArray = numpy.array(image2BCleared)
width, height = image2BCleared.size
newData = []
counterI = 0
counterJ = 0
for item in imageArray:
    if item.all == (0, 0, 0, 225):
        newData.append((255, 255, 255, 0))
        print("true")
    else:
        newData.append(item)
print(newData)

output:

[array([[  0,   0,   0, 255],
       [  0,   0,   0, 255],
       [  0,   0,   0, 255],
       [  0,   0,   0, 255],
      [  0,   0,   0, 255],
#fowllowed by other pixels from different colors but nothing transparent

basically "newData" has a lot of slots that contains (0, 0, 0, 255). I'm not very familiar with image processing so it might be some really basic error but I don't really see any logic problems (turn picture into array of pixels, compare pixels to a certain value, if color == (0, 0, 0, 255) add a transparent pixel to newData else add the pixel to newData)

any help would be appreciated

Aucun commentaire:

Enregistrer un commentaire