Image Manipulation in Python. Step by step guide to commonly used... | by Behic Guven | Towards Data Science

In this post, I will show you how to edit an image using Python. The process of editing an image is called image manipulation. You might be wondering why you need to do some touches on your images before using them in your projects. There are many reasons for this, but a couple of main reasons can be listed as saving storage space, improving the quality of training, and faster running time. The manipulation techniques that will be covered in this post are image resizing, image brightness, and lastly converting the image color to grayscale. We will do some hands-on practice on an image by testing each of them.
For one image it doesn’t make much difference but when you think of processing thousands of images in your program, little size change will save you a lot of time and storage.
It’s easy for us to understand what we see when we look at an image, but for machines, it can be a bit challenging. So, to help machines to process the image better, a little brightness can help to improve the accuracy.
When training a machine, grayscale images are doing much better. The reason is since machines see images as a matrix of arrays, it is easier to store black and white image, instead of multiple images with many colors.
These are just some of the image manipulation techniques we will cover in this post. There are many more, but as mentioned earlier, these are the most commonly used techniques and can be applied to any format of image. If you are ready, let’s started!
Before we get to coding, let’s choose an image that we want to test our codes with. This image can be in any format, but I would suggest using ‘png’ or ‘jpg’. Here is the image I choose to play with. The good thing about this image is no filters or effects are used.
We will only use one package in total. The main library that we will use for image manipulation is called PIL, which is the image processing library. PIL will be installed as ‘pillow’, don’t get confused, they are the same thing.
Let’s start by installing the package then.
pip install pillow
It’s time to import the package as libraries so that we can use them.
from PIL import Image, ImageEnhance
I’ve renamed my image as ‘testme.jpg’, and in this step, I am importing the image by assigning it to a variable name called ‘img’. We will do the most commonly used three manipulation techniques. To make things clean and simple, I will use the same image for each technique, but feel free to define different images for each manipulation technique.
img = Image.open("testme.jpg")
The first thing we will try on this image is resizing it. Resizing is very easy using PIL library, which is an image processing library as mentioned earlier. After resizing the image, I want to save it so that we can see the image after we run the program. And the end to save the image as an image file, we will use ‘save’ method.
Before we resize, you can print the current size of your image by the following code.
print(img.size)
Resizing the image:
newsize = (300, 300) img_resized = img.resize(newsize) print(img_resized.size)
Now, we can save the image file in our folder.
img_resized.save("resized.jpg")
Secondly, let’s add some light to our image. Especially with shadowed images, adding brightness with help the machine to see the lines in the image better. This is very important when training a machine, because machines accept things as it is, and little misinformation can cause wrong training. Anyways, here is the code to add some brightness:
# Define enhancerenhancer = ImageEnhance.Brightness(img) img_light = enhancer.enhance(1.8) img_light.save("brightened.jpg")
How about for the images with too much light, can we do something for them? Of course, like adding light, there is also a way to get some light out. This will be helpful for images taken under a sunny day. To do this is very easy, we will just change the value between the parenthesis. If the number is more than 1, the enhancer will add light. If the value is between 0 and 1, the enhancer will get some light out, which will make the image darker.
As the value gets smaller, the darker it gets. You can play with the value, and watch how it acts.
enhancer = ImageEnhance.Brightness(img) img_light = enhancer.enhance(0.8) img_light.save("darkened.jpg")
As our third image manipulation techniques, we will see how to convert an image to greyscale. In other words, we can say black and white. Storing a grayscale image instead of a multiple colored images is more efficient, and it’s easier for a machine to understand. Especially, when training the machine to learn a specific object in an image, greyscale is one of the most commonly used technique to start with.
img = Image.open('testme.jpg') img = img.convert('L') img.save('grayscaled.jpg')
These are some of the most commonly used image manipulation techniques. These tricks will help you to edit images in a faster and easier way. In this exercise, we edited just one image, but it’s possible to run the same code in a loop, this way you will be able to edit thousands of images in couple lines of code.
I am Behic Guven, and I love sharing stories on creativity, programming, motivation, and life.
Follow my blog and Towards Data Science to stay inspired.