In this tutorial we will check how to flip an image, using Python and OpenCV. We will flip the image across the x-axis, the y-axis and then across both axes.
Introduction
In this tutorial we will check how to flip an image, using Python and OpenCV. We will flip the image across the x-axis, the y-axis and then across both axes.
This tutorial was tested on Windows 8.1, using Python version 3.7.2 and OpenCV version 4.0.0.
The code
As usual, we will start our code by importing the cv2 module.
import cv2
Then we will load the image by calling the imread function of the cv2 module. As input, we will pass the file system path to the image we want to use.
originalImage = cv2.imread('C:/Users/N/Desktop/Test.jpg')
Then we will start flipping the images by calling the flip function from the cv2 module. As first input this function receives the image to which we want to apply the flipping. As second input it receives an integer representing the flip operation we want [1].
We can pass the following values to the second parameter [1]:
- 0, for flipping the image around the x-axis (vertical flipping);
- > 0 for flipping around the y-axis (horizontal flipping);
- < 0 for flipping around both axes.
So, we will start by flipping the image around the x-axis, by passing the value 0 as second argument of the flip function.
flipVertical = cv2.flip(originalImage, 0)
Then we will flip the image around the y-axis, by passing a value greater than 0. We will use the value 1.
flipHorizontal = cv2.flip(originalImage, 1)
Then we will flip the image around both axes, by passing a value lesser than 0. We will use the value -1.
flipBoth = cv2.flip(originalImage, -1)
After applying the transformation to the image and obtaining the results, we will display each one in a different window. We will also display the original image, for comparison.
As we covered in previous tutorials, we display an image by calling the imshow function of the cv2 module, passing as first input a string with the name of the window and as second input the image to be displayed.
cv2.imshow('Original image', originalImage)
cv2.imshow('Flipped vertical image', flipVertical)
cv2.imshow('Flipped horizontal image', flipHorizontal)
cv2.imshow('Flipped both image', flipBoth)
To finalize, we will wait for a key to be pressed by the user and then we will destroy all the windows we previously opened.
cv2.waitKey(0)
cv2.destroyAllWindows()
The final code can be seen below.
import cv2
originalImage = cv2.imread('C:/Users/N/Desktop/Test.jpg')
flipVertical = cv2.flip(originalImage, 0)
flipHorizontal = cv2.flip(originalImage, 1)
flipBoth = cv2.flip(originalImage, -1)
cv2.imshow('Original image', originalImage)
cv2.imshow('Flipped vertical image', flipVertical)
cv2.imshow('Flipped horizontal image', flipHorizontal)
cv2.imshow('Flipped both image', flipBoth)
cv2.waitKey(0)
cv2.destroyAllWindows()
Testing the code
To test the code, simply run the previous script using a tool of your choice. I’ve used IDLE, a Python IDE.
You should get an output similar to figure 1, which shows the original image and the 3 other versions, after applying the different types of flipping.

References
[1]https://docs.opencv.org/4.0.0/d2/de8/group__core__array.html#gaca7be533e3dac7feb70fc60635adf441