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.
OPENING IMAGE FILES
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: img1=Image.open('flower.jpg')
(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).
ROTATING AN IMAGE
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: img1=img1.rotate(90)
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: img1.save('rotated.jpg')
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:
img1=img1.rotate(90) img1.save('rotated.jpg')
CROPPING AN IMAGE
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:
width,height=img1.size
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) img1.crop(area) img1.save('cropped.jpg')
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'.
RESIZING AN IMAGE
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: img1=img1.resize((width/2,height/2))
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: img1=img1.resize((floor(width/2),floor(height/2))) We are putting width/2 and height/2 in floor functions, converting the results to integers, if not already to "error proof" our program.)
PASTING AN IMAGE ON TOP OF ANOTHER
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 img2=Image.open('bird.jpg)
Now to paste this picture of a bird, or img2 on top of img1 ("flower.jpg") we do the following: img1.paste(img2,(50,50))
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: img1.save('pasted.jpg')
FLIPPING IMAGES
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=img1.transpose(PIL.Image.FLIP_TOP_BOTTOM) img1.save('flipped vertical.jpg')
This is used for flipping img1 vertically. To flip the image horizontally, we would do something similar:
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.
OPENING IMAGE FILES
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 calledimg1
. Now to do this, the following code would be written:img1=Image.open('flower.jpg')
(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).ROTATING AN IMAGE
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 rotateimg1
or "flower.jpg" 90 degrees, we need type up the following code:img1=img1.rotate(90)
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:img1.save('rotated.jpg')
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:img1=img1.rotate(90)
img1.save('rotated.jpg')
CROPPING AN IMAGE
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:width,height=img1.size
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 variablearea
: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)
img1.crop(area)
img1.save('cropped.jpg')
Here we are specifying the amount we crop in the first line, using the
.crop
function and passing inarea
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'.RESIZING AN IMAGE
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:
img1=img1.resize((width/2,height/2))
We are here specifying how much smaller we want the image to be, in this case, 2 times smaller as we are dividing
height
andwidth
(variable declared in the previous section) by 2.(NOTE- If the answer to
width/2
orheight/2
is afloat
type, Python will throw an error at you. In this case, just importmath
and do this:img1=img1.resize((floor(width/2),floor(height/2)))
We are putting
width/2
andheight/2
infloor
functions, converting the results to integers, if not already to "error proof" our program.)PASTING AN IMAGE ON TOP OF ANOTHER
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 doimg2=Image.open('bird.jpg)
Now to paste this picture of a bird, or
img2
on top ofimg1
("flower.jpg") we do the following:img1.paste(img2,(50,50))
This line of code uses the
.paste
function which takes 2 parameters. The first is the name of the image being pasted on top ofimg1
, and the second being a tuple containingpx
values, which specify the distance from the side and top ofimg1
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:img1.save('pasted.jpg')
FLIPPING IMAGES
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 thePIL
library.So if we want to flip
img1
vertically, we copy up the following:img1=img1.transpose(PIL.Image.FLIP_TOP_BOTTOM)
img1.save('flipped vertical.jpg')
This is used for flipping
img1
vertically. To flip the image horizontally, we would do something similar:img1=img1.transpose(PIL.Image.FLIP_LEFT_RIGHT)
img1.save('flipped horizontal.jpg')
THE END
To see these things in action, you can click the links below:
https://repl.it/talk/share/Photo-editor-with-python/49187
https://repl.it/talk/share/Image-flipper/49531
Comments, upvotes and feedback are appreciated
Hope you liked it :)
np @userSM