Python Image Library PIL - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Python Image Library PIL

Description:

Scaling, cropping, compositing, transforms, bands (e.g. alpha channel) ... cutoutIm = myImage.crop (bounds) Paste. PIL Slicing & Dicing. Blend/composite. Rotate ... – PowerPoint PPT presentation

Number of Views:742
Avg rating:3.0/5.0
Slides: 15
Provided by: Bri8325
Category:

less

Transcript and Presenter's Notes

Title: Python Image Library PIL


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
  • gtgtgt import Image
  • gtgtgt im Image.open(vacation.jpeg")
  • gtgtgt newIm Image.new (RGBA, (640, 480), (255,
    0, 0))
  • Format file format
  • gtgtgt print im.mode, im.size, im.format
  • RGB (1024, 768) JPEG
  • gtgtgt im.save (vacation.png) OR
  • gtgtgt im.save (vacation.png, PNG)

5
PIL API - Concepts
  • Resampling Filters
  • NEAREST, BILINEAR, BICUBIC, ANTIALIAS
  • gtgtgt 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
  • gtgtgt bands im.split ()
  • gtgtgt rIm bands 0
  • gtgtgt aIm bands 3
  • gtgtgt remadeImage Image.merge (RGBA, (rIm, gIm,
    bIm, aIm))
  • gtgtgt grayscaleIm Image.open (myAlpha.gif)
  • gtgtgt remadeImage myImage.putalpha (grayscaleIm)
    replace the alpha band

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

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

10
PIL - Drawing
  • ImageDraw module creates drawing surface for
    image
  • gtgtgt import Image, ImageDraw
  • gtgtgt im Image.open(vacation.jpeg")
  • gtgtgt drawSurface ImageDraw.Draw (im)
  • gtgtgt import Image, ImageDraw
  • gtgtgt im Image.open(vacation.jpeg")
  • gtgtgt im.getpixel ( (4,4) )
  • gtgtgt 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
  • fillfillColor
  • outlineoutlineColor

12
PIL Drawing Text
  • Text
  • gtgtgt drawSurface ImageDraw.Draw (im)
  • gtgtgt drawable.text ((10, 10), Hello,
    fill(255,0,0), fontNone)
  • TrueType Font supportgt
  • gtgtgt import ImageFont
  • gtgtgt ttFont ImageFont.truetype (arial.ttf, 16)
  • gtgtgt drawable.text ((10, 10), Hello,
    fill(255,0,0), fontttFont)

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
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com