Python Image Library PIL 10/13/05. PIL – What is it? Pythonic library for Loading and Saving...
-
Upload
jayson-mckinney -
Category
Documents
-
view
237 -
download
3
Transcript of Python Image Library PIL 10/13/05. PIL – What is it? Pythonic library for Loading and Saving...
Python Image LibraryPIL
10/13/05
PIL – What is it?
Pythonic library forLoading 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)
PIL - Examples
Image resizing or format conversionsAdding save/load support into your application* Font support also handy
Chart generation (also see PIDDLE piddle.sf.net)Thumbnail generationWatermarking or adding legend
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”)
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
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%)”
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
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
PIL – Slicing & Dicing
Blend/composite
Rotate
Transpose ROTATE_90/180/270, FLIP_TOP_BOTTOM, FLIP_LEFT_RIGHT >>> fixedIm = myImage.transpose (ROTATE_90)
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))
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
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)
References
Python Image Libraryhttp://www.pythonware.com/products/pil/
Other documentation sourcesPIL Handbookhttp://effbot.org/imagingbook/Nice pdf from New Mexico Techhttp://www.nmt.edu/tcc/help/pubs/pil/
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)