In this tutorial, we will use an example to show you how to use cv2.flip() to flip image in python opencv.
1.Open an image
import cv2 img = cv2.imread('data/src/lena.jpg') print(type(img)) # <class 'numpy.ndarray'> print(img.shape) # (225, 400, 3)
2.Use cv2.flip() to flip images
We should notice:
cv2.flip() flip images according to the value of flipCode as follows:
- flipcode = 0: flip vertically
- flipcode > 0: flip horizontally
- flipcode < 0: flip vertically and horizontally
Here is an example:
img_flip_ud = cv2.flip(img, 0) cv2.imwrite('data/dst/lena_cv_flip_ud.jpg', img_flip_ud) # True img_flip_lr = cv2.flip(img, 1) cv2.imwrite('data/dst/lena_cv_flip_lr.jpg', img_flip_lr) # True img_flip_ud_lr = cv2.flip(img, -1) cv2.imwrite('data/dst/lena_cv_flip_ud_lr.jpg', img_flip_ud_lr) # True
Run this code, you may find this resultant image: