These are the instructions:
Write a function called zeroLowBits that has as input argument the file name of an image file, then sets the lower 4 bits of each pixel (red, green, and blue components) of the image to zero. Save the new image in a file called filename+“_zerolowbits_mod.gif” Your function will display the original image first, and then modified image with low bit set to zero. Notice that the new image looks very similar to the original.
This is my code, but it doesnt give the right output. it just show the original picture many times.
def zeroLowBits(filename): myImage = Image.open(filename) myImage = myImage.convert('RGB') width, height = myImage.size myImage.show() for x in range(width): for y in range(height): r,b,g = myImage.getpixel((x,y)) newr = r & 0b0000 newg = g & 0b0000 newb = b & 0b0000 myImage.putpixel((x,y),(newr,newg,newb)) newfilename = filename[:-4] newfilename = newfilename + "_zerolowbits_mod.gif" myImage.save(newfilename) myImage.show(newfilename)
I have attached the picture. On the left is the original picture but on the right is the picture I am trying to get as the output. Any Suggestions?