I have an assignnment to write a code in python. These are the instructions:
Write a function called shiftHighBits that has two input arguments. The first one is the file name of an image file; the second one is the maxshift value. Your function will first display the original image and then do the following:
Repeat for each k in range 1 to maxshift:
1. Iterate through every pixel in the image and right shift the color bits by k bits.
2. Save the transformed image in a file having as name: filename+“shiftbits”+str(k)+“mod.gif”
So far I have this much of my code. I get the original picture and then I get an all black picture which is the picture that I am suppose to get last but I don't think I am doing the "Iterate through every pixel in the image and right shift the color bits by k bits." part correctly in my 3rd for loop. Any suggestions?
def shiftHighBits(filename,maxshift): myImage = Image.open(filename) myImage = myImage.convert('RGB') width, height = myImage.size myImage.show() for x in range(width): for y in range(height): for k in range(1,maxshift): r,b,g = myImage.getpixel((x,y)) myImage.putpixel((x,y),(r>>k,g>>k,b>>k)) newfilename = filename[:-4] newfilename = newfilename + "_shiftbits"+str(k)+"mod.gif" myImage.save(newfilename) myImage.show()