Have you ever felt stuck trying to convert, resize, apply a filter to images with the aid of a design tool like Photoshop or Illustrator? With Python, we can achieve this with just basic script written in our favourite code editors or IDEs.
Python is one of the most popular programming languages and it is favoured amongst beginners trying to experiment with their first language. Python provides lots of libraries for image processing but we would focus our attention with Python Imaging Library (PIL). PIL helps to perform basic operations on images like resizing, rotation, creating thumbnails, convert between different file formats etc.
In this tutorial, we are going to assume you have a basic understanding of Python and its syntax to be able to follow. Before we begin the installation of PIL, ensure you have the latest Python installed on your system python3
.
Installation
The first step in working with image processing is to install the required library, Pillow. We will use pip
to install the required library from the terminal or command line.
$pip3 install pillow
That's it. Now we can begin.
Open, rotate and display and image
The following script loads an image, rotate it 90 degrees, and display an image on the default system image viewer.
from PIL import Image
img = Image.open("bulbasaur.jpg")
img.rotate(90).show()
Create thumbnails
The thumbnail method, thumbnail() helps to maintain the aspect ratio of images especially when you want it to scale it down/up proportionally. In between the method, enter the dimension you want.
Something you should take note of is that the thumbnail method doesn't return a new image, it just modifies the current one.
from PIL import Image
img = Image.open("bulbasaur.jpg")
img.thumbnail((400, 250))
img.show()
Apply filter to images.
Here we would sharpen, blur, and smoothen an image based on our preference. With this process, we have to import another module, ImageFilter to the pre-existing Image module used to represent a PIL image.
Replace BLUR attribute with SHARPEN and SMOOTH to see the changes to the processed image.
from PIL import Image, ImageFilter
img = Image.open("bulbasaur.jpg")
filtered_img = img.filter(ImageFilter.BLUR)
filtered_img.show()
Convert and save
We can change the format of an image from one form to another.
You can either create a new folder or save the image in the same folder where you want to apply the save() method to.
from PIL import Image
img = Image.open("bulbasaur.jpg")
img.save("/images/processed/new_bulbasaur.png", "png")
Convert image to grayscale
We would be making use of the convert() method to apply changes to an image from its original coloured image to grey.
from PIL import Image
img = Image.open("bulbasaur.jpg")
filtered_img = img.convert("L")
filtered_img.show()
The "L" stands for 'luminous'
Resize image
With this method, resize(), you can resize your image with the following script.
from PIL import Image
img = Image.open("bulbasaur.jpg")
resize = img.resize((300, 300))
resize.show()
Crop image
The crop() method returns a rectangular region from the cropped image. The box is a tuple of the crop rectangle which takes in four input as coordinates, (left, upper, right, lower) represented as figures.
from PIL import Image
img = Image.open("bulbasaur.jpg")
box = (100, 100, 250, 250)
region = img.crop(box)
region.show()
Read the image
You can read the image through its various attributes such as mode, size, and format. We can use the Python Print() function.
Replace the size
attribute with filter
and mode
attributes.
from PIL import Image
img = Image.open("bulbasaur.jpg")
print(img.size)
Conclusion
That's it from my end! Remember you can do more exciting things with reading the Pillow documentation and we have barely scratched the surface of endless possibilities we can achieve using this Python library.
If this helped you in any way, kindly leave a feedback or comment below.
Further reading
Image effects with PIL(Python Image Library)
Essential Pil (Pillow) Image Tutorial (for Machine Learning People)
Top comments (4)
If you want to get familiar with python it's good but for actual usage; image-magick has a CLI tool that does much more than this. imagemagick.org/script/convert.php
I would look into it.
Thanks
Cool very useful, thanks man😁✌️👍
Glad you found it useful.