I use PIL to make an image with a black background and make a mask out of it. What I want the program to do is iterate over all the pixels of the image and if the pixel is black it makes it white and if it is some other color it makes it black, but I'm not sure how to compare the pixel values correctly so that determine what to do with the pixel.
Here is my code so far that creates the entire black image.
import os, sys
import Image
filename = "C:\Users\pdiffley\Dropbox\C++2\Code\Test\BallSpriteImage.bmp"
height = 50
width = 50
im = Image.open(filename)
im = im.load()
i = 0
j = 0
while i<height:
while j<width:
if im[j,i] == (0,0,0):
im[j,i] = (255,255,255)
else:
im[j,i] = (0,0,0)
j = j+1
i = i+1
mask = Image.new('RGB', (width, height))
newfile = filename.partition('.')
newfile = newfile[0] + "Mask.bmp"
mask.save(newfile)
I believe the problem lies in the if statement, comparing im [j, i] with the RGB value (0,0,0), which is always evaluated as false. What is the correct way to compare a pixel?
source
share