Here are the assignment instructions:
Write a function called blackAndWhite that has as input argument the file name of an image file (example: bluefish.gif), then transforms the image in black and white and displays it. In order to create a black and white image, do:
1. Prepare the image file: (i.e open the image file and convert it into RGB representation)
2. Display the initial image
3. Loop over each pixel in the image (hint: you'll need a nested for loop) and compute the average of r, g, b. Next, update the pixel value with average.
4. Save the new image in a file called: filename”_BW_mod.gif”
5. Display the black and white image on screen
#below is what I have so far in my code. I think I generally understand steps 1,2,4 and 5 but not really #3
def blackandwhite(filename)
myImage = Image.open(filename)
myImage = myImage.convert('RGB')
myImage.show()
# in here I am trying to figure out the nested loop and how to start it
newfilename = filename[:-4]
newfilename = newfilename + "_BW_mod.gif"
myImage.save(newfilename)
myImage.show(newfilename)
any suggestions?

New Topic/Question
Reply



MultiQuote





|