#!/usr/bin/env python # coding: utf-8 # In[9]: from PIL import Image import matplotlib.pyplot as plt import urllib, cStringIO get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: def getImgFromURL(url): f = cStringIO.StringIO(urllib.urlopen(url).read()) image = Image.open(f) return image def getImg(s): if s.startswith('http'): image=getImgFromURL(s) else: image = Image.open(s) plt.imshow(image) return image # In[3]: url="http://3.bp.blogspot.com/_6NwFMYOnaGg/TN9ZUnWjKGI/AAAAAAAAABc/569lxw9cDrY/s320/GB97.jpg" #url='pennyRed.jpeg' url='cover.jpg' image=getImg(url) image # In[6]: def getCancellation(image,R=130,G=130,B=130): pix = image.load() xmax,ymax=image.size out = Image.new(image.mode, (xmax,ymax), None) opixel = out.load() for x in xrange(image.size[0]): for y in xrange(image.size[1]): (a,b,c)=pix[x,y] if aR-ish and gG-ish and bB-ish: if flip: opixel[x, y]=(255,255,255) else: opixel[x, y]=(0,0,0) else: if flip: opixel[x, y]=(0,0,0) else:opixel[x, y]=(255,255,255) plt.imshow(out) return out # In[ ]: def removeColour(image,R=130,G=130,B=130,ish=10,flip=False): pix = image.load() xmax,ymax=image.size out = Image.new(image.mode, (xmax,ymax), None) opixel = out.load() for x in xrange(image.size[0]): for y in xrange(image.size[1]): (r,g,b)=pix[x,y] if rR-ish and gG-ish and bB-ish: if flip: opixel[x, y]=(r,g,b) else: opixel[x, y]=(255,255,255) else: if flip: opixel[x, y]=(255,255,255) else:opixel[x, y]=(0,0,0)#(r,g,b) plt.imshow(out) return out # In[ ]: removeColour(image,194,154,150,35,True) # In[ ]: getCancellationIsh(image,120,110,90,15) # In[ ]: from PIL import ImageFilter plt.imshow(c.filter(ImageFilter.BLUR)) # In[ ]: plt.imshow(c.filter(ImageFilter.CONTOUR)) # In[ ]: plt.imshow(c.filter(ImageFilter.DETAIL)) # In[ ]: plt.imshow(c.filter(ImageFilter.EDGE_ENHANCE)) # In[ ]: plt.imshow(c.filter(ImageFilter.EDGE_ENHANCE_MORE)) # In[ ]: plt.imshow(c.filter(ImageFilter.EMBOSS)) # In[ ]: plt.imshow(c.filter(ImageFilter.FIND_EDGES)) # In[ ]: plt.imshow(c.filter(ImageFilter.SMOOTH)) # In[ ]: plt.imshow(c.filter(ImageFilter.SMOOTH_MORE)) # In[ ]: plt.imshow(c.filter(ImageFilter.SHARPEN)) # In[ ]: plt.imshow(c.filter(ImageFilter.MedianFilter(size=3))) # In[ ]: plt.imshow(c.filter(ImageFilter.ModeFilter(size=3))) # In[ ]: def makeMask(image): pix = image.load() xmax,ymax=image.size mask = Image.new(image.mode, (xmax,ymax), None) mask.convert('RGBA') opixel = mask.load() for x in xrange(image.size[0]): for y in xrange(image.size[1]): (a,b,c)=pix[x,y] if a<130 and b<130 and c<130: opixel[x, y]=(0,0,0,0) else: opixel[x, y]=(255,255,255,255) plt.imshow(mask) return mask # In[ ]: mask=makeMask(image) # In[ ]: plt.imshow(image) # In[ ]: def highlight(image,R=90,G=80,B=90): pix = image.load() xmax,ymax=image.size ma = Image.new(image.mode, (xmax,ymax), None) opixel = ma.load() for x in xrange(image.size[0]): for y in xrange(image.size[1]): (a,b,c)=pix[x,y] if a>R and b>G and c>B: opixel[x, y]=(250,0,0) else: opixel[x, y]=(255,255,255)#pix[x,y]#(255,255,255) #plt.imshow(ma) return ma highlight(image,10,100,100) # In[ ]: def blankCancellation(image): pix = image.load() xmax,ymax=image.size ma = Image.new(image.mode, (xmax,ymax), None) opixel = ma.load() for x in xrange(image.size[0]): for y in xrange(image.size[1]): (a,b,c)=pix[x,y] if a<130 and b<130 and c<130: opixel[x, y]=(230,220,180) else: opixel[x, y]=pix[x,y]#(255,255,255) #plt.imshow(ma) return ma # In[ ]: blankCancellation(image) # In[ ]: #Generate list of pixels to fill in from mask def getPixels(ma): pixels=[] for x in xrange(image.size[0]): for y in xrange(image.size[1]): (a,b,c)=pix[x,y] if a<130 and b<130 and c<130: pixels.append([x,y]) return pixels #Find valid neighboring pixels: in the image and a non-background colour (so choose a clashy background colour) def inBounds(x,y): if x>0 and y>0 and x= 0 and c < max): return True else: return False def neighbor_pixels(x, y, img): "Find all valid neighboring pixels" neighbors = [] if (is_valid(y+1, height) and img[x, y+1][0] == 0): neighbors.append([x, y+1]) if (is_valid(y-1, height) and img[x, y-1][0] == 0): neighbors.append([x, y-1]) if (is_valid(x+1,width) and img[x+1, y][0] == 0): neighbors.append([x+1, y]) if (is_valid(x-1,width) and img[x-1, y][0] == 0): neighbors.append([x-1, y]) return neighbors def extract_alpha(img): "Create a list of pixels [[x,y],...] for a given image where pixels are not null" alpha = [] y = 0 while y < height: x = 0 while x < width: if (img[x,y][0] == 0): alpha.append([x,y]) x = x + 1 y = y + 1 shuffle (alpha) return alpha def average_rgb(pixels, img): "For a given list of pixels [[x,y],...], return the average color as RGB tuple" r, g, b = 0, 0, 0 for p in pixels: c = img[p[0], p[1]] #print(c) r += c[0] g += c[1] b += c[2] length = len(pixels) if length > 0: return (int(r/length), int(g/length), int(b/length)) else: return (0, 0, 0) alpha = extract_alpha(ma) x=1 if len(alpha)!=0: print('interp',len(alpha)) while len(alpha) >0: #print(len(alpha)) for p in alpha: neighbors = neighbor_pixels(p[0], p[1], ma) if len(neighbors) > 0: #print(neighbors,average_rgb(neighbors, im)) im[p[0], p[1]] = average_rgb(neighbors, im) tma[p[0], p[1]] = (1,1,1) ma = tma alpha = extract_alpha(ma) else: print('no interp') newfile = 'testfill.jpg'#sys.argv[3] image.save(newfile) plt.imshow(image) # In[ ]: im[99,0] # In[ ]: pix=ma p=0 q=0 r=0 for x in xrange(image.size[0]): for y in xrange(image.size[1]): (a,b,c,d)=pix[x,y] if a==255: p=p+1 elif a==0: q=q+1 else: r=r+1 print(p,q,r) # In[ ]: image.size # In[ ]: 127*134 # In[ ]: