Are you a Pythonista? if yes, then you should have tried many automation sprits using Python. But, have you ever tried photo editing in python?. Yes, using simple python scripts, you can easily edit your photos. Today, in this tutorial, let’s see how we can do this using Pillow.
1. Introduction to Pillow
- Pillow is a third-party Python library that is used to manipulate Images. It is a Python Imaging Library, also known as PIL.
- Using this library you can read, manipulate and save back the images using python.
- Pillow adds the image processing capability to our python interpreter. Alex Clark is one of the prominent names in the list of Pillow contributors.
- Pillow offers robust image processing capabilities along with support for multiple file formats.
2. Install Pillow
To get started, we have to install the pillow library into python. You can run the below code which installs this library into python using PiP.
#Install Pillow pip install pillow #import pillow from PIL import Image, ImageFilter
By this, Pillow should be installed and imported into python. Let’s rock!!!
3. Image Blur
Image blurring is nothing but reducing the sharpness of the image. One of the major applications of the image blue is that you can reduce the noise.
Along with noise reduction, this will indirectly reduce the size of the image. Let’s see how we can do this using python.
#Image blur #load the image my_image = Image.open('lion.jpg') #Use the blur function image_blur = img.filter(ImageFilter.BLUR) #Save the image blur.save('lion_blur.jpg')
I hope the difference is highly visible.
4. Image Sharpen
You can use this method to sharpen the images. It basically enriches the definition of edges. In other words, if an image is appearing dull, then its edges are not sharp.
Let’s see how we can do this using pillow in python.
#Image sharpen # Load the image my_image = Image.open('lion.jpg') #Use sharpen function sharp = img.filter(ImageFilter.SHARPEN) #Save the image sharp.save('image_sharpen.jpg')
You can see the more sharp image now.
5. Image Flip
Image flipping or reversal is ideally a time-reversed image on a horizontal axis. In simple words, it will be a mirrored image of the original image.
Let’s see how quickly and easily we can flip an image in python.
#Image flip #load the image img = Image.open('lion.jpg') #use the flip function flip = img.transpose(Image.FLIP_LEFT_RIGHT) #save the image flip.save('image_flip.jpg')
Looks pretty cool!
6. Image Greyscale
The image is grey scaled when you remove all other color information from the image. If you do so, you will leave with either dark black or bright white.
A simple difference between RGB and Greyscale images is, that RGB has 3 color channels and Grey has only one color channel.
Let’s see how we can convert an image into greyscale in python.
#grey scale image #load the image my_imgage = Image.open('Lion.jpg') #use convert function convert = img.convert('L') #Save the image convert.save('grey_scale.jpg')
Lion looks Amazing now!
Photo Editing In Python – Conclusion
Photo editing in python using pillow library is very easy and lightning fast. The library offers the simple function to manipulate the image and you can process the image in multiple ways. I hope you loved this topic as much as I did. That’s all for now. Happy Python!!!
More read – Photo Editing tools for Python