1 / 14

Python Image Library PIL

Python Image Library PIL. 10/13/05. PIL – What is it?. Pythonic library for Loading and Saving images (diverse formats) Scaling, cropping, compositing, transforms, bands (e.g. alpha channel) Drawing text and basic shapes

wauna
Download Presentation

Python Image Library PIL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Image LibraryPIL 10/13/05

  2. PIL – What is it? • Pythonic library for • Loading and Saving images (diverse formats) • Scaling, cropping, compositing, transforms, bands (e.g. alpha channel) • Drawing text and basic shapes • Akin to the program GIMP (*GIMP also has a nice python API called GIMP-Python)

  3. 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

  4. PIL API - Concepts • Image – from file or newly created • >>> import Image • >>> im = Image.open(“vacation.jpeg") • >>> newIm = Image.new (“RGBA”, (640, 480), (255, 0, 0)) • Format – file format • >>> print im.mode, im.size, im.format • RGB (1024, 768) JPEG • >>> im.save (“vacation.png”) OR • >>> im.save (“vacation.png”, “PNG”)

  5. PIL API - Concepts • Resampling Filters • NEAREST, BILINEAR, BICUBIC, ANTIALIAS • >>> smallIm = im.resize ( (128, 128), Image.ANTIALIAS) • Sizes – tuple of width and height • Bounding boxes – tuples of x1, y1, x2, y2 • 0,0 is always upper left

  6. PIL API - Concepts • Mode – gray scale “L”, color “RGBA” “RGB”, etc. • 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 (3x8-bit pixels, true colour) • RGBA (4x8-bit pixels, true colour with transparency mask) • CMYK (4x8-bit pixels, colour separation) • YCbCr (3x8-bit pixels, colour video format) • I (32-bit integer pixels) • F (32-bit floating point pixels) • Color – specified as 32bit value, tuple, or string • myColor = (255, 0, 0, 128) # full red, with 50% alpha • myColor = 0x7f0000ff # full red, with 50% alpha • myColor = “#00ff00” # web color • myColor = “rgb (75%, 0%, 0%)” • myColor = “hsl (0, 100%, 50%)”

  7. PIL API - Concepts • Bands – the separate color channels • >>> bands = im.split () • >>> rIm = bands [0] • >>> aIm = bands [3] • >>> remadeImage = Image.merge (“RGBA”, (rIm, gIm, bIm, aIm)) • >>> grayscaleIm = Image.open (“myAlpha.gif”) • >>> remadeImage = myImage.putalpha (grayscaleIm) # replace the alpha band

  8. PIL – Slicing & Dicing • Thumbnails • Modifies in-place • Preserves aspect ratio • >>> myImage.thumbnail ((128, 128), Image.ANTIALIAS) • Crop • >>> bounds = (100, 100, 400, 400) • >>> cutoutIm = myImage.crop (bounds) • Paste

  9. PIL – Slicing & Dicing • Blend/composite • Rotate • Transpose • ROTATE_90/180/270, FLIP_TOP_BOTTOM, FLIP_LEFT_RIGHT • >>> fixedIm = myImage.transpose (ROTATE_90)

  10. PIL - Drawing • ImageDraw module creates drawing surface for image • >>> import Image, ImageDraw • >>> im = Image.open(“vacation.jpeg") • >>> drawSurface = ImageDraw.Draw (im) • >>> import Image, ImageDraw • >>> im = Image.open(“vacation.jpeg") • >>> im.getpixel ( (4,4) ) • >>> im.putpixel ( (4,4), (255,0,0))

  11. PIL - Drawing • Basic methods of drawing surface • chord arc pieslice (bounds, strtAng, endAng) • ellipse (bounds) • line (coordSeq) • point (coordSeq) • polygon (coordSeq) • rectangle (bounds) # first coord inclusive, second coord exclusvie • Common optional args for these methods • fill=fillColor • outline=outlineColor

  12. PIL – Drawing Text • Text • >>> drawSurface = ImageDraw.Draw (im) • >>> drawable.text ((10, 10), “Hello”, fill=(255,0,0), font=None) • TrueType Font support> • >>> import ImageFont • >>> ttFont = ImageFont.truetype (“arial.ttf”, 16) • >>> drawable.text ((10, 10), “Hello”, fill=(255,0,0), font=ttFont)

  13. References • Python Image Library • http://www.pythonware.com/products/pil/ • Other documentation sources • PIL Handbook • http://effbot.org/imagingbook/ • Nice pdf from New Mexico Tech • http://www.nmt.edu/tcc/help/pubs/pil/

  14. BMP BUFR (identify only) EPS (write-only) FITS (identify only) GIF GRIB (identify only) HDF5 (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)

More Related