MCS 275 Spring 2023
Emily Dumas
Reminders and announcements:
It is a package you can install with pip
.
python3 -m pip install pillow
Or check the official install instructions.
Pillow is the name of the package (what you ask pip
to install).
The module you import to use it is called PIL
.
The name difference is for historical reasons.
Pillow offers a drop-in replacement for a different module called PIL
,
"Python Imaging Module", that was abandoned in 2011.
There are two basic types of image files you will encounter: vector images and bitmap images.
Store instructions about what to draw (a circle here, a line there, etc.); can be viewed at any size without loss of sharpness.
Displaying a vector image is a complicated operation! (PS, PDF are full programming languages with recursion, iteration, etc.)
A rectangular grid of colors, meant to be displayed with each color corresponding to one pixel on the display device; becomes blurry or blocky if you zoom in.
JPEG is for photos. That's what the P stands for.
JPEG compresses the image data, discarding some of it. Images with sharp edges will look bad as a result.
Use PNG for anything other than photos, unless it is essential to have a small file size.
JPEG is an amazing application of the Fourier transform, and I think everyone should learn a bit about it. I used all the self-control I could muster to say no more about it here.
Pillow is for working with bitmap images. It can read and write PNG, JPEG, GIF, BMP, TIFF, and more.
It is useful for format conversion, low-level image operations (e.g. make this pixel red), and provides some high-level operations too (e.g. blur, sharpen, convert to grayscale, ...).
PIL.Image
is a class that represents bitmap images.
from PIL import Image # just import PIL is not enough!
img = Image.open("adorable_kitten.png") # load
img.save("discord_avatar.jpg") # save
# new color image, 1920x1080 resolution, all red
img = Image.new("RGB",(1920,1080),color=(255,0,0))
An image file can store various amounts and types of color data. Pillow encodes this in a mode string:
"1"
- 1 bit per pixel, 0=black, 1=white"L"
- 8 bits per pixel, 256 shades of gray. 0=black, 255=white. Also called
"grayscale"."RGB"
- 24 bits per pixel, 8 each for red, green, blue. Also called "true color".
Most common.These are common modes, but there are lots more. Some images also have transparency information (A or "alpha" channel).
A location in a bitmap image is specified by a pair of integers (x,y)
. The upper left
corner is (0,0)
. Coordinate x
increases as you move right, and
y
increases as you move down.
Note the y
direction is opposite from mathematics.
Suppose img
is a PIL.Image
object.
Set a pixel color (draw a tiny dot):
# make pixel at (10,20) magenta
img.putpixel( (10,20), (255,0,255) )
Get a pixel color:
# returns color of pixel at (10,20)
img.getpixel( (10,20) )
Some other methods of PIL.Image
:
convert
- Conversion to a different mode, e.g. from true color to grayscale.crop
- Crop (remove all but a smaller rectangle).resize
- Stretch or compress to a new size.paste
- Draw another image on this one.transpose
- Do any combination of mirroring and rotating by multiples of 90
degrees.Lots more in the documentation.
If you have PIL image objects frame0
, frame1
, frame2
, etc.
frame0.save(
"anim.gif",
save_all=True,
append_images=[frame1,frame2,...],
duration=50,
loop=0
)
duration
is milliseconds per frame
loop=0
means loop forever (loop=5
means play 5 times and stop)
There are many Python image processing libraries, and for a particular purpose it may be best to use something other than Pillow. Examples:
numpy
arrays extensively.Generally, Pillow tends to emphasize minimal dependencies and doing basic things well.
GUI image editing tools are good for making modifications to an image that require planning, decisions, review, revision, etc.
PIL and other programmatic image manipulation libraries are great for batch operations and cases where the input or output of a program is naturally an image.