Not_Linked_In

Pillow image manipulation

The Pillow library of Python is a fork of the older PIL (Python Imaging Library) project. It allows easy scripted manipulation of many image types. Essentially I'm using it mostly just to remove image meta data like GPS coordinates, and to crop and resize, but it can do much more.

Code Block Code Desc
from PIL import Image
with Image.open('<localfile.jpg>') as img: Using with ensures that original is closed immediately after loading
     img.load() Load local image to memory
img.format, img.size, img.mode Show file type, show image size in pixels, show mode such as RGB
img.width, img.height Show width, height in pixels
crop1 = img.crop((<start_x>,<start_y>,<end_x>,<end_y>)) x/y is horizontal/vertical with origin in upper left
resize1 = crop1.resize((crop1.width // 4, crop1.height // 4)) Reduce x/y resolution by factor of 4
rotate1 = resize1.rotate(n) Rotate image counter-clockwise n degrees
Cameras default to landscape, images taken in portrait will be oriented wrong.
Rotate can be used to reorient these, but use the additional argument 'expand=1' to preserve dimensions
rotate1.save('<newlocalfile.jpg>') Write modified image to local
rotate1_thumb = rotate1.thumbnail((x,y)) Create an image with x,y max dimensions.
This is handy as it will preserve dimensions, useful for making thumbnails.
exif_dict = dict(<img>._getexif().items()) Capture metadata to dictionary
Use the link at bottom to lookup tags in the dict using dec ID column
Note that many tags won't be available. Use try/except to attempt to load data from dict.