Python Image Library PIL 101305 PIL What is
Python Image Library PIL 10/13/05
PIL – What is it? Pythonic library for Loading and Saving images (diverse formats) l Scaling, cropping, compositing, transforms, bands (e. g. alpha channel) l Drawing text and basic shapes l Akin to the program GIMP (*GIMP also has a nice python API called GIMP-Python)
PIL - Examples Image resizing or format conversions Adding save/load support into your application * Font support also handy Chart generation (also see PIDDLE piddle. sf. net) Thumbnail generation Watermarking or adding legend
PIL API - Concepts Image – from file or newly created >>> import Image >>> im = Image. open(“vacation. jpeg") >>> new. Im = Image. new (“RGBA”, (640, 480), (255, 0, 0)) Format – file format >>> RGB >>> print im. mode, im. size, im. format (1024, 768) JPEG im. save (“vacation. png”) OR im. save (“vacation. png”, “PNG”)
PIL API - Concepts Resampling Filters l NEAREST, BILINEAR, BICUBIC, ANTIALIAS >>> small. Im = im. resize ( (128, 128), Image. ANTIALIAS) Sizes – tuple of width and height Bounding boxes – tuples of x 1, y 1, x 2, y 2 l 0, 0 is always upper left
PIL API - Concepts Mode – gray scale “L”, color “RGBA” “RGB”, etc. l l l l l 1 (1 -bit pixels, black and white, stored with one pixel per byte) L (8 -bit pixels, black and white) P (8 -bit pixels, mapped to any other mode using a colour palette) RGB (3 x 8 -bit pixels, true colour) RGBA (4 x 8 -bit pixels, true colour with transparency mask) CMYK (4 x 8 -bit pixels, colour separation) YCb. Cr (3 x 8 -bit pixels, colour video format) I (32 -bit integer pixels) F (32 -bit floating point pixels) Color – specified as 32 bit value, tuple, or string l l l my. Color = = = (255, 0, 0, 128) 0 x 7 f 0000 ff “#00 ff 00” “rgb (75%, 0%)” “hsl (0, 100%, 50%)” # full red, with 50% alpha # web color
PIL API - Concepts Bands – the separate color channels >>> >>> bands = im. split () r. Im = bands [0] a. Im = bands [3] remade. Image = Image. merge (“RGBA”, (r. Im, g. Im, b. Im, a. Im)) >>> grayscale. Im = Image. open (“my. Alpha. gif”) >>> remade. Image = my. Image. putalpha (grayscale. Im) # replace the alpha band
PIL – Slicing & Dicing Thumbnails l l Modifies in-place Preserves aspect ratio >>> my. Image. thumbnail ((128, 128), Image. ANTIALIAS) Crop >>> bounds = (100, 400, 400) >>> cutout. Im = my. Image. crop (bounds) Paste
PIL – Slicing & Dicing Blend/composite Rotate Transpose l ROTATE_90/180/270, FLIP_TOP_BOTTOM, FLIP_LEFT_RIGHT l >>> fixed. Im = my. Image. transpose (ROTATE_90)
PIL - Drawing Image. Draw module creates drawing surface for image >>> import Image, Image. Draw >>> im = Image. open(“vacation. jpeg") >>> draw. Surface = Image. Draw (im) >>> >>> import Image, Image. Draw im = Image. open(“vacation. jpeg") im. getpixel ( (4, 4) ) im. putpixel ( (4, 4), (255, 0, 0))
PIL - Drawing Basic methods of drawing surface l l l chord arc pieslice (bounds, strt. Ang, end. Ang) ellipse (bounds) line (coord. Seq) point (coord. Seq) polygon (coord. Seq) rectangle (bounds) # first coord inclusive, second coord exclusvie Common optional args for these methods l l fill=fill. Color outline=outline. Color
PIL – Drawing Text >>> draw. Surface = Image. Draw (im) >>> drawable. text ((10, 10), “Hello”, fill=(255, 0, 0), font=None) True. Type Font support > >>> import Image. Font >>> tt. Font = Image. Font. truetype (“arial. ttf”, 16) >>> drawable. text ((10, 10), “Hello”, fill=(255, 0, 0), font=tt. Font)
References Python Image Library l http: //www. pythonware. com/products/pil/ Other documentation sources PIL Handbook l http: //effbot. org/imagingbook/ l Nice pdf from New Mexico Tech l http: //www. nmt. edu/tcc/help/pubs/pil/ l
BMP BUFR (identify only) EPS (write-only) FITS (identify only) GIF GRIB (identify only) HDF 5 (identify only) IM JPEG MPEG (identify only) MSP PALM (write only) PCX PDF (write only) PNG PPM TIFF WMF (identify only) XBM XV Thumbnails CUR (read only) DCX (read only) FLI, FLC (read only) FPX (read only) GBR (read only) GD (read only) ICO (read only) IMT (read only) IPTC/NAA (read only) MCIDAS (read only) MIC (read only) PCD (read only) PIXAR (read only) PSD (read only) SGI (read only) TGA (read only) WAL (read only) XPM (read only)
- Slides: 14