You can flip an image in OpenCV Python by following the given steps. I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn Computer Vision.
Open the Spyder IDE (integrated development environment).
Import the OpenCV library. If OpenCV is not installed in your system then first install it using This Method.
import cv2 #cv2 is used for OpenCV library
Now read the image from the location. In my case “F:\\AiHints” is the location and “top30.jpg” is the name of the image. Change it according to your image location and name.
image = cv2.imread("F:\\AiHints\\top30.jpg") #imread is use to read an image from a location
In this step, I will flip the above image vertically and save it with the new name “VFlip.jpg”.
cv2.flip(image, 0) # vertically flip cv2.imwrite('F:\\AiHints\\VFlip.jpg',image)
In this step, I will flip the image horizontally and save it with the new name “HFlip.jpg”.
cv2.flip(image, 1) # horizontally flip cv2.imwrite('F:\\AiHints\\HFlip.jpg',image)
In this step, I will flip the image horizontally and vertically and save it with the new name “H AND V Flip.jpg”.
cv2.flip(image, -1) # horizontally and vertically flip cv2.imwrite('F:\\AiHints\\H AND V Flip.jpg',image)
waitKey() open the image for a specific time in milliseconds until you press any key. “destroyAllWindows()” will destroy all the windows that we created.