Learn to Code via Tutorials on Repl.it!

← Back to all posts
Image manipulation with Python
userSM (260)

Image manipulation is not the first thing that comes to mind when thinking about Python due to Python being mainly terminal based. But today I'm hoping to show you how to perform basic image editing and create a basic image editor!

IMPORTING (the module, obviously)

To perform actions upon image files, you need to import the Python Imaging Library (PIL). From there you need to import the Image method. So the following code would be used:
from PIL import Image.
Done, the first step to your own photo editor has been completed.


You cannot open image files as you would open .txt files. To open an image file, we need to assign the image to a variable, so it is easy for use to edit it using Python. Let's open an image file called flower.jpg and assign it to a variable called img1. Now to do this, the following code would be written:

(I'm opening "flower.jpg", but you can pass in the image name of any image file you want to edit as the parameter for the open function).


This is the start of the fun stuff. To rotate an image, in this case "flower.jpg", we need to remember the variable name of that which we assigned the image file. In this case, it's called img1. To rotate img1 or "flower.jpg" 90 degrees, we need type up the following code:

This code is taking img1 and rotating it 90 degrees (you can pass in any numerical value as the parameter). But we need to actually see the changed version, so we need to save the modified image as a different image file, so we need the following:

This code is saving the rotated version of "flower.jpg" as "rotated.jpg" (a new image file).

So to rotate img1 90 degrees and save this new image as "rotated.jpg", we would need the following block of code:



To crop an image, in this case, img1, we need to assign to variables, width,height to the width and height of your images. To get the width and height of an image and assign them to their respective variables, we need to do the following:


The .size function returns a tuple value of the width and height of an image. Now that we've done this, we need to create a variable area:
area = (0,0,width/2,height/2)

To crop this image, say halfway through the width and height we do type the following code:
area = (0,0,width/2,height/2)

Here we are specifying the amount we crop in the first line, using the .crop function and passing in area as the tuple value which contains the amount that we need to crop, and finally saving the cropped version as a separate image file called 'cropped.jpg'.


Sometimes we need to resize an image to make it smaller, so it has a higher resolution, or for some other reasons. To do this we just type:

We are here specifying how much smaller we want the image to be, in this case, 2 times smaller as we are dividing height and width (variable declared in the previous section) by 2.

(NOTE- If the answer to width/2 or height/2 is a float type, Python will throw an error at you. In this case, just import math and do this:
We are putting width/2 and height/2 in floor functions, converting the results to integers, if not already to "error proof" our program.)


To do this, we need to open another image file. I will open a file called "bird.jpg". I will assign this the to a variable called img2. To open this (refer back to first section in unsure), we would do

Now to paste this picture of a bird, or img2 on top of img1 ("flower.jpg") we do the following:

This line of code uses the .paste function which takes 2 parameters. The first is the name of the image being pasted on top of img1, and the second being a tuple containing px values, which specify the distance from the side and top of img1 to the new image, img2. After this, we just need to save the pasted image, by doing the following, in the same method in which we have used so far:


The last section of this tutorial, this is a bit different from the others. To flip an image, go back to the top of your code, and type in import PIL. This will import the rest of the PIL library.

So if we want to flip img1 vertically, we copy up the following:
img1.save('flipped vertical.jpg')

This is used for flipping img1 vertically. To flip the image horizontally, we would do something similar:

img1.save('flipped horizontal.jpg')


To see these things in action, you can click the links below:


Comments, upvotes and feedback are appreciated

Hope you liked it :)

RickiTomphson (0)

Excellent! By the way, I would like to know where do you process the photos? Your processing style reminded me of mine. I process my photos using this software - https://photolemur.com . Have you ever heard of Photolemur?

Jgame (0)

Can we use png files?