- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to flip an image in OpenCV Python?
In OpenCV, an image can be flipped using the function cv2.flip(). Using this function we can flip the image across X-axis, Y-axis and across both axes. It accepts a flag flipCode as an argument to flip the image across the axis.
If the flipCode is set to 0, the image is flipped across the x-axis and if the flipCode is set to a positive integer (say 1), the image is flipped across the Y-axis. If the flipCode is set to a negative integer (say "-1"), the image is flipped across both axes.
Steps
To flip an image, one could follow the steps given below −
Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed it.
Read the input image using cv2.imread() method. Specify full path of image with the image type (i.e. png or jpg)
Apply cv2.flip() function on the input image img. Pass the parameter flipCode for desired flip. We set flipCode=0 to flip around the x-axis.
img_v = cv2.flip(img, 0)
Display the flipped output image.
We will use this image as the Input File in the following examples −
Example
In this Python program, we flip the input image across x-axis (vertically).
# import required library import cv2 # read input image img = cv2.imread('blue-car.jpg') # flip the image by vertically img_v = cv2.flip(img, 0) # display the rotated image cv2.imshow("Vertical Flip", img_v) cv2.waitKey(0) cv2.destroyAllWindows()
Output
When you run the above program, it will produce the following output window −
Notice the output image is flipped across the X-axis.
Example
In this Python program, we flip the input image across the y-axis (horizontally).
# import required library import cv2 # read input image img = cv2.imread('blue-car.jpg') # flip the image by horizontally img_h = cv2.flip(img, 1) # display the rotated image cv2.imshow("Horizontal Flip", img_h) cv2.waitKey(0) cv2.destroyAllWindows()
Output
When you run the above program, it will produce the following output window −
Notice the output image is flipped across the Y-axis.
Example
In this Python program, we flip the input image across both axes (vertically as well as horizontally).
# import required library import cv2 # read input image img = cv2.imread('blue-car.jpg') # rotate the image both vertically and horizontally img_vh = cv2.flip(img, -1) # display the rotated image cv2.imshow("Both vertical and horizontal flip", img_vh) cv2.waitKey(0) cv2.destroyAllWindows()
Output
When you run the above program, it will produce the following output window −
Notice the output image is flipped across the X-axis and well as Y-axis.
- Related Articles
- How to flip an image using Java OpenCV library?
- How to read an image in Python OpenCV?
- How to mask an image in OpenCV Python?
- How to normalize an image in OpenCV Python?
- How to rotate an image in OpenCV Python?
- How to resize an image in OpenCV using Python?
- How to flip an image in Node Jimp?
- How to detect humans in an image in OpenCV Python?
- OpenCV Python – How to add borders to an image?
- How to blur faces in an image using OpenCV Python?
- How to detect eyes in an image using OpenCV Python?
- How to convert an RGB image to HSV image using OpenCV Python?
- Using OpenCV in Python to Cartoonize an Image
- How to draw an arrowed line on an image in OpenCV Python?
- OpenCV Python Program to blur an image?
