In [10]:
from IPython.core.display import Image
Image(r'C:\Users\David\Documents\Dropbox\Projects Image\anonymous side.jpg')
Out[10]:
In [9]:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
%matplotlib inline
img = mpimg.imread(r'C:\Users\David\Documents\Dropbox\Projects Image\anonymous side.jpg')
imgplot = plt.imshow(img)
plt.axis('off')
Out[9]:
In [ ]:
import Image
background = Image.open("test1.png")
foreground = Image.open("test2.png")
background.paste(foreground, (0, 0), foreground)
background.show()
#First parameter to .paste() is the image to paste.
#Second are coordinates, and the secret sauce
#is the third parameter. It indicates a mask that
#will be used to paste the image. If you pass a
#image with transparency, then the alpha channel
#is used as mask.
im.save("filename")
In [ ]:
from PIL import Image, ImageDraw
im = Image.open("lena.pgm")
draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
del draw
# write to stdout
im.save(sys.stdout, "PNG")
In [ ]:
#Copying a subrectangle from an image
box = (100, 100, 400, 400)
region = im.crop(box)
#Processing a subrectangle, and pasting it back
region = region.transpose(Image.ROTATE_180)
im.paste(region, box)
In [ ]:
# Simple geometry transforms
out = im.resize((128, 128), resample=...)
#PIL.Image.NEAREST, .BILINEAR, .BICUBIC .ANTIALIAS (default nearest)
out = im.rotate(45, resample=0, expand=0)
# degrees counter-clockwise
# expand increases canvas size
# Transposing an image
out = im.transpose(Image.FLIP_LEFT_RIGHT)
out = im.transpose(Image.FLIP_TOP_BOTTOM)
out = im.transpose(Image.ROTATE_90)
out = im.transpose(Image.ROTATE_180)
out = im.transpose(Image.ROTATE_270)
In [ ]:
# split, merge channels
r, g, b = im.split()
im = Image.merge("RGB", (b, g, r))
im = im.quantize(colors=256, method=None, kmeans=0, palette=None) #researcha bit first
In [ ]:
# paste
im.paste(clip, box=None, mask=None) (not im=)
# box=(l,u) or (l,u,r,b), None=(0,0)
#source can be image or integet or tuple containing pixel values, fills with color
In [ ]:
new = im.copy()
sel = im.crop([l, u, r, b]) # lazy, view might be updated, might not; call load method to break connection
l, u, r, b = im.getbbox() #bounding box of non-zero pixel values
seqobj = im.getdata(band="R") # onject; for list, list(im.getdata...)
((rl, rh), (gl, gh), (bl, bh)) = im.getextrema() # max and min pixel vals
(r, g, b) = getpixel((x, y))
new = im.offset(xoffset, yoffset) # copy with data wrapped around edges
In [ ]:
#draw
from PIL import Image, ImageDraw
im = Image.open("lena.pgm")
draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)
del draw
# write to stdout
im.save(sys.stdout, "PNG")
##############################
.ellipse((l, u, r, b), fill=None, outline=None(Color)
.line([(x, y), (x, y)...] or [x, y, x, y...]), fill+None, width=0 (wide looks bad because joining)
.polygon (like line, but draws line between first and last)
.rectange(like ellipse)
.text((x,y), "text", fill=None, font=None, anchor=None)
im = im.putdate(seqobj, scale=1.0, offset=0.0)
im = im.putpixel((x, y), value)
In [ ]:
a_list = histogram(mask+None, extrema=None)
In [ ]:
im = Image.new(mode, size, color=0)
# Mode = RGB or L (Greyscale)
# size = (w, h)
# color = int or tuple
new = composite(image1, image2, mask)
#convert mode:
new = im.convert("L")
In [ ]:
ImageOps module
PIL.ImageOps.Colorize(im, color tuple to map black, color tuple to map white)
.equalize(image, mask=None) # equal greyscale valyues
.fit(image, (w,h), resample=0, bleed=0 (dont use), centering=(0.5, 0.5) like the box with arrows in photoshop)
.grayscale(im)
.invert(im)
.flip(im) vert
.mirror(im) horiz
ImageFilter module
from PIL import ImageFilter
im1 = im.filter(ImageFilter.BLUR)
im2 = im.filter(ImageFilter.MinFilter(3))
im3 = im.filter(ImageFilter.MinFilter) # same as MinFilter(3)
.GaussianBlur(radius=2)
.UnsharpMask(radius=2, percent=150, threshold=3)
.RankFilter(size of kernel, rank 0=min, size^2/2 median, size^2-1 max)
.ModeFilter(size=3)
In [ ]:
ImageEnhance module
from PIL import ImageEnhance
enhancer = ImageEnhance.Sharpness(image)
for i in range(8): factor = i / 4.0 enhancer.enhance(factor).show("Sharpness %f" % factor)
#factor = 1.0 for no change, lower=less, higher=more (no restrictions? 0-1-2?)
#.Color(0=b&w, 1= orig, presumable >1=saturated) .Contrast .Brightness .Sharpness (0-1-2)
In [ ]:
ImageChops module (CHannel OPerations, eg multiply, add, etc)
PIL.ImageChops.composite(im1, im2, mask) like Image.composite()
see http://pillow.readthedocs.org/en/latest/reference/ImageChops.html
screen, subtract, lots
In [ ]:
In [ ]:
In [ ]: