We are looking into PIL library lately, so I thought it could be nice to have a sort of cheat sheet of the functions and classes we’ve used in our code.
What is PIL
PIL is a powerful module for Python that allows you to create and elaborate images by conding in Python. You can do almost anything, building the perfect image tools for your needs.
In this tutorial you will be guided through the most interesting tools you can use with a lot of code examples, to avoid being stuck with theory.
Install
First you need to install pil’s fork pillow:
pip install pillow
Import
from PIL import Image
Create
img = Image.new(‘RBG’, (600,400), ‘yellow’)
Open
img = Image.open(‘existing.png’)
Save
img.save(‘myimage.png’)
Show
img.show()
Resize
img.resize((100,100), Image.ANTIALIAS)
Blur (from PIL import ImageFilter)
img.filter.(ImageFilter.BLUR)
Blend 2 images together
img = Image.blend(Image.open(‘image1.png’,’image2.png’, 0.5))
Pasting an image on another
img.paste((0,0),’image2.png’)
Write text on an image (ImageDraw)
draw = ImageDraw.Draw(img)
draw.text(0,0,’This text goes on top of the image’)