Image Manipulation in Python

In this post, I will show you how to edit an image using Python. This process editing an image is called Image Manipulation. You might be wondering why you need to do some manipulations on images before using them in your code. There are many reasons for this question, but couple main reasons can be listed as:

  • Image Resize
    • 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.
  • Image Brightness
    • 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.
  • Image Greyscale
    • When training a machine, grayscale images are doing much better. The reason is since machines sees images as matrix of arrays, it is easier to store black and white image, instead of multiple image with many colors.

These are some of the image manipulation techniques I will cover in this post. There are many more, but as mentioned earlier, these are mostly used techniques, and can be applied to any kind of image. If you are ready, let’s started!

Preparation

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. Good thing about this image is no filters or effects are used.

Packages

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 packages then.

pip install opencv-python pillow

It’s time to import the package as libraries so that we can use them.

from PIL import Image, ImageEnhance

Define an Image

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")

Technique 1: Resize

First thing we will try on this image is resizing it. Resizing is very easy using PIL library, which is 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")

Result

300×300 pixels

Technique 2: Brightness

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 accepts things as it is, and little misinformation can cause wrong training. Anyways, here is the code to add some brightness:

enhancer = ImageEnhance.Brightness(img) 
img_light = enhancer.enhance(1.8) 
img_light.save("brightened.jpg")

Result

Brightened

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 more 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")

Result

Darkened

Technique 3: Grayscale

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 multiple colored image is more efficient, and it’s easier for machine to understand. Especially, when training the machine to learn a specific object in an image, greyscale is one the most commonly used technique to start with.

img = Image.open('testme.jpg')
img = img.convert('L')
img.save('grayscaled.jpg')

Result

Grayscaled

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 image in couple lines of code. Follow me to stay inspired with more machine learning projects.

Thank you,

I am Behic Guven, and I love sharing stories on creativity, programming, motivation, and life. Let’s be friends on Medium.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s