Image Manipulation Using Python

Introduction

 
Have you ever come across a situation where you need to edit or modify multiple images?
 
It is very tedious to work with each image one by one. If you need to apply the same changes to all the images, that is very time consuming and boring. But, using Python, you can do it quite quickly.
 
Let’s start playing with images.
 

Reading images and getting image details

 
Let’s see how we can read an image. The following is my solution structure.
 
Image Manipulation Using Python 
 
I have a Python file named ImageManipulation.py and an image file named PythonImageFromGoogle.PNG in my folder.
 
To read the image present in the folder, you can use the following code.
  1. #from PIL import Image,ImageFilter  
  2. #To read from image and get image details  
  3. pythinImg = Image.open('PythonImageFromGoogle.PNG')  
  4. print(pythinImg.size)  
  5. print(pythinImg.format)  
Here, I am trying to import an image from PIL. I will be using the Image filter in the following examples that I have imported with it now.
 
Size and Format will get you the size and format type of the image respectively.
 
Output
 
Image Manipulation Using Python
 

How to create a new image?

 
It is easy to create a new image in Python.
  1. #let's create a new image to play around  
  2. newImage = Image.new('RGBA',(200,150),'blue')  
  3. newImage.save('newImage.png')  
“Image.new” will create a new image with width 200 and height 150. The image colour is blue.
 
"Save" will save the image in the same folder with the name provided here.
 
Output
 
Following is the folder structure after the execution. You can notice the newImage.png added in the folder.
 
Image Manipulation Using Python
 

Rotate the image and open it

 
You can rotate an image by whatever angle you want to rotate and save the image.
  1. #rotate the existing image  
  2. pythinImg.rotate(180).save('PythonImageFromGoogle180.PNG')  
  3. pythinImg_180 = Image.open('PythonImageFromGoogle180.PNG')  
  4. pythinImg_180.show()  
Here, the first line rotates the image by 180 degrees and saves the file. And in the third line, the rotated images open up.
 
Output
 
Following is the folder structure after the execution. You can notice the PythonImageFromGoogle180.PNG added in the folder.
 
Image Manipulation Using Python
  

Filter an Image

 
It is easy to apply a filter on images to make them visually better.
  1. #Applying a filter to the image  
  2. image_sharp = pythinImg_180.filter( ImageFilter.SHARPEN )  
  3. #Saving the filtered image to a new file  
  4. image_sharp.save( 'PythonImageFromGoogle180Sharpen.PNG' )  
Output
 
Following is the folder structure after the execution. You can notice the PythonImageFromGoogle180Sharpen.PNG added in the folder.
 
Image Manipulation Using Python
  1. # Complete code   
  2. from PIL import Image,ImageFilter  
  3.   
  4. #To read from image and get image details  
  5. pythinImg = Image.open('PythonImageFromGoogle.PNG')  
  6. print(pythinImg.size)  
  7. print(pythinImg.format)  
  8.   
  9. #let's create a new image to play around  
  10. newImage = Image.new('RGBA',(200,150),'blue')  
  11. newImage.save('newImage.png')  
  12.   
  13. #rotate the existing image  
  14. pythinImg.rotate(180).save('PythonImageFromGoogle180.PNG')  
  15. pythinImg_180 = Image.open('PythonImageFromGoogle180.PNG')  
  16. pythinImg_180.show()  
  17.   
  18. #Applying a filter to the image  
  19. image_sharp = pythinImg_180.filter( ImageFilter.SHARPEN )  
  20. #Saving the filtered image to a new file  
  21. image_sharp.save( 'PythonImageFromGoogle180Sharpen.PNG' )  
Output
 
Image Manipulation Using Python
 
Image Manipulation Using Python 
 
Please try it out, you will enjoy it. Happy coding!!!


Similar Articles