Introduction
In this article, I will show, how you can Flip your image Horizontally and Vertically using Python. To do this job, we will use a Python Image Processing library called Pillow.
Not only you can flip your images, but also can rotate your images with the help of the Pillow library. I have created a project already for it which is called Image Rotator Application. The User Interface is pretty straightforward and is managed by the Tkinter library. If interested, check this out also from there.
So, without wasting any more time, let's get straight into the main topic.
👉Visit Also: Hack with Image: Extract Metadata from an Image using Python
Requirements
Since we are discussing a python topic, you must have installed Python on your system. Despite that, you will need to install the Pillow library if you haven't already. The below command will help you to complete your installation process.
Use pip3 instead of pip for Linux.
☛Install Pillow: pip install Pillow
The Image
To show you the output properly, I will use the below image in the rest of the program.
Flip Horizontally
The below code will help you to rotate your image horizontally(left-to-right or right-to-left direction). You just need to mention your image path properly in the program(see the yellow line). We will pass this Image.FLIP_LEFT_RIGHT constant to the transpose() method to flip an image horizontally.
from PIL import Image
# Opening the image
img = Image.open("sample.jpg")
# Flip Horizontally
horizontal_flipped = img.transpose(Image.FLIP_LEFT_RIGHT)
# Displaying the image
horizontal_flipped.show()
Output
Horizontally Flipped Image |
Save the Image
The previous code can only flip an image horizontally but there we didn't save that flipped image after the operation. You can do that too just using a single line of code which is given below.
# Save the resulting image
horizontal_flipped.save('result_1.jpg')
Flip Vertically
We successfully flipped an image horizontally. In that case, we did this left to right(right-to-left case can be possible using the same program) direction. Let's do the same task but horizontally. In our case, the direction will be bottom-to-top but top-to-bottom can be done using the same program.
Here, we will pass this Image.FLIP_TOP_BOTTOM constant to the transpose() method to flip an image vertically. Let's jump into the code directly.
from PIL import Image
# Opening the image
img = Image.open("sample.jpg")
# Flip Vertically
vertical_flipped = img.transpose(Image.FLIP_TOP_BOTTOM)
# Displaying the image
vertical_flipped.show()
Output
Vertically Flipped Image |
Save the Image
Again, you can save your flipped image if you wish using this single line of python code below.
# Save the resulting image
vertical_flipped.save('result_2.jpg')
Output through video
👉Visit Also: Extract Text from an Image using a Python Program
Summary
In this tutorial, you learned how you can flip your image horizontally(left-to-right or right-to-left) and vertically(top-to-bottom or bottom-to-top) using just a few lines of python code.
Later we saved our flipped images. We used an image processing library provided by Python called Pillow for this job. Not only this task but there are also many Python Projects I developed where Image Processing played a major role. Some of them are given below. I will recommend you to visit those also to know the versatility of python more precisely.
👉Image Viewer Application using Python Tkinter
👉Add Your Watermark on a Photo using Python
👉Convert an Image to a Cartoon using Python - Tkinter Project
👉Image to Pencil Sketch Converter in Python - Tkinter Project
That's all for today, See you on the next topic soon.
Wish You Have a nice day ahead, cheers!
PySeek