## Purdue CS 177 Image Manipulation Project ## Written by Samantha Feulner, Created 7/15/2012 ## Last Modified 7/25/2012 from PIL import Image ##Open and display images A, B, and C def openFirst(A): im1 = Image.open(A) im1.show() return im1 def openAll(A, B, C): im1 = Image.open(A) im2 = Image.open(B) im3 = Image.open(C) im1.show() im2.show() im3.show() ##Create a black and white copy of an image and display with the original def blackAndWhite (image): ##Open the image X0 = Image.open(image) ##Remember to convert the image to RGB when editing the pixels X0 = X0.convert('RGB') width, height = X0.size X0.show() for x in range(height): ##Nested for loop iterates through all the pixels for y in range(width): r, g, b = X0.getpixel((y, x)) brightness = int(round(0.33*r + 0.33*g + 0.33*b) ) X0.putpixel((y, x), (brightness, brightness, brightness)) ## Save the modified image newPre = image[:-4] cfileName = newPre+'_BW_mod.gif' X0.save(cfileName) X0.show() ##Removes green from every pixel in the image def noGreen(image): X0 = Image.open(image) X0 = X0.convert('RGB') width, height = X0.size X0.show() for x in range(height): for y in range(width): r, g, b = X0.getpixel((y, x)) X0.putpixel((y, x), (r, 0, b)) newPre = image[:-4] cfileName = newPre+'_NoGreen_mod.gif' X0.save(cfileName) X0.show() ##The lowest four bits of each color component in the pixels are set to zero def zeroLowBits (image): X0 = Image.open(image) X0 = X0.convert('RGB') width, height = X0.size X0.show() X1 = X0.copy() for x in range(height): for y in range(width): r, g, b = X0.getpixel((y, x)) r = r & int('11110000',2) g = g & int('11110000',2) b = b & int('11110000',2) X1.putpixel((y, x), (r, g, b)) newPre = image[:-4] cfileName = newPre+'_Zero4LowBits_mod.gif' X1.save(cfileName) X1.show() ##The bits of each color component in the pixels are shifted by k bits to the right def shiftBitsLeft (image, shift): X0 = Image.open(image) X0 = X0.convert('RGB') X0.show() X1 = X0.copy() width, height = X0.size for x in range(height): for y in range(width): r, g, b = X0.getpixel((y, x)) r = (r << shift) & 255 g = (g << shift) & 255 b = (b << shift) & 255 X1.putpixel((y, x), (r, g , b)) X1.show() newPre = image[:-4] cfileName = newPre+'_ShiftLeft'+str(shift)+'_mod.gif' X1.save(cfileName) ##The bits of each color component in the pixels are shifted by k bits to the right def shiftBitsRight (image, shift): X0 = Image.open(image) X0 = X0.convert('RGB') X0.show() X1 = X0.copy() width, height = X0.size for x in range(height): for y in range(width): r, g, b = X0.getpixel((y, x)) r = r >> shift g = g >> shift b = b >> shift X1.putpixel((y, x), (r, g , b)) X1.show() newPre = image[:-4] cfileName = newPre+'_ShiftRight'+str(shift)+'_mod.gif' X1.save(cfileName) # check hiding op def chk1(pa,ps,pa1,ps1,co): if pa>=256 or pa<0: 1/0 if ps>=256 or ps<0: 2/0 if co>=256 or co<0: 3/0 if pa1 != 0: if pa1<16: 4/0 if ps1>=16 or ps1<0: 5/0 sh = ps - (ps1<<4) if sh > 16 or sh<0: 6/0 ##Hides picture S in picture A by placing the four higher bits of S as the four lower bits of A def hidePicture(A, S): antique0 = Image.open(A) antique0 = antique0.convert('RGB') awidth, aheight = antique0.size antique0.show() smuggle0 = Image.open(S) smuggle0 = smuggle0.convert('RGB') swidth, sheight = smuggle0.size smuggle0.show() combine = Image.new('RGB',(awidth,aheight)) if awidth !=swidth: print("Images must be equal in size") elif aheight != sheight: print("Images must be equal in size") else: for x in range(aheight): for y in range(awidth): rs, gs, bs = smuggle0.getpixel((y, x)) if (rs & 0xFF) != rs: 1/0 ra, ga, ba = antique0.getpixel((y, x)) if (ra == rs) and (ga==gs) and (ba==bs): print('.',sep='',end='') # shift high bits of smuggled to low bits rs = rs >> 4 gs = gs >> 4 bs = bs >> 4 ra = ra & 0xF0 ga = gs & 0xF0 ba = ba & 0xF0 ra1 = ra+rs ga1 = ga+gs ba1 = ba+bs ## if ((ra1 & 0xF0)>>4) == rs1: ## print('.',sep='',end='') combine.putpixel((y, x),(ra1, ga1, ba1)) newPre = A[:-4] cfileName = newPre+'_pregnant_mod.gif' combine.save(cfileName) combine.show() ##extract image of low-order 4 bits by left shift of 4 def extractPicture (image): shift = 4 X0 = Image.open(image) X0 = X0.convert('RGB') X0.show() width, height = X0.size print("w",width,"h",height) X1 = Image.new('RGB',(width,height)) X1.show() for x in range(height): for y in range(width): r, g, b = X0.getpixel((y, x)) r1=r&15 g1=g&15 b1=b&15 r2 = (r1 << shift) & 255 g2 = (g1 << shift) & 255 b2 = (b1 << shift) & 255 X1.putpixel((y, x), (r2, g2 , b2)) if (r&0x0F)==((r&0xF0)>>4): print('.') X1.show() newPre = image[:-4] cfileName = newPre+'_extracted_mod.gif' X1.save(cfileName) ##Morphs two images using the Image.blend function and displays the morphing steps in a loop def morphPicture (image1, image2): X1 = Image.open(image1) X2 = Image.open(image2) x1width, x1height = X1.size x2width, x2height = X2.size if x1width != x2width: print("Images must be equal in size") elif x1height != x2height: print("Images must be equal in size") else: for x in range (0, 17, 1): combine = Image.blend(X1, X2, x/16) combine.show() def control(): print("Operations:") print(" 0 - exit") print(" 1 - zero 4 low-order bits") print(" 2 - shift left") print(" 3 - shift right") print(" 4 - hide picture") print(" 5 - extract picture") print(" 6 - zero lower 4") code = eval(input("choice: ")) if code == 0: return code, 0 else: src = input("file name: ") return code, src def zeroLow4(fname): X0 = Image.open(fname) X0 = X0.convert('RGB') X0.show() width, height = X0.size for x in range(height): for y in range(width): r, g, b = X0.getpixel((y, x)) r = r >> 4 g = g >> 4 b = b >> 4 X0.putpixel((y, x), (r, g , b)) X0.show() for x in range(height): for y in range(width): r, g, b = X0.getpixel((y, x)) r = r << 4 g = g << 4 b = b << 4 X0.putpixel((y, x), (r, g , b)) X0.show() newPre = fname[:-4] cfileName = newPre+'_ZeroLow'+'_mod.gif' X0.save(cfileName) def main(): todo, fname = control() while todo != 0: if todo == 1: zeroLowBits(fname) elif todo == 2: k = eval(input('enter shift: ')) shiftBitsLeft(fname,k) elif todo == 3: k = eval(input('enter shift: ')) shiftBitsRight(fname,k) elif todo == 4: fname2 = input('hidden file name: ') hidePicture(fname, fname2) elif todo == 5: extractPicture(fname) elif todo == 6: zeroLowBits(fname) else: print("unknown choice") todo, fname = control() ## openAll("X1.gif", "X2.gif", "X3.gif") ## blackAndWhite("X2.gif") ## noGreen("GreenFish.gif") ## zeroLowBits("X1.gif") ## shiftHighBits("X3.gif", 5) ## hidePicture("X1.gif", "X2.gif") ## morphPicture("X1.gif", "X2.gif") main()