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.

Syntax

cv2.flip(image, flipCode)

Parameters

  • image: Input image to be flipped
  • flipCode: Integer flag specifying flip direction
flipCode Description Effect
0 Flip around x-axis Vertical flip (upside down)
1 Flip around y-axis Horizontal flip (mirror)
-1 Flip around both axes 180° rotation

Vertical Flip (flipCode = 0)

In this example, we flip the input image across x-axis (vertically) ?

import cv2
import numpy as np

# Create a sample image for demonstration
image = np.zeros((100, 150, 3), dtype=np.uint8)
image[10:40, 20:80] = [255, 0, 0]  # Blue rectangle
image[60:90, 70:130] = [0, 255, 0]  # Green rectangle

print("Original image shape:", image.shape)

# Flip the image vertically (across x-axis)
img_vertical = cv2.flip(image, 0)

print("Flipped image shape:", img_vertical.shape)
print("Vertical flip completed successfully")
Original image shape: (100, 150, 3)
Flipped image shape: (100, 150, 3)
Vertical flip completed successfully

Horizontal Flip (flipCode = 1)

In this example, we flip the input image across the y-axis (horizontally) ?

import cv2
import numpy as np

# Create a sample image
image = np.zeros((100, 150, 3), dtype=np.uint8)
image[10:40, 20:80] = [255, 0, 0]  # Blue rectangle
image[60:90, 70:130] = [0, 255, 0]  # Green rectangle

# Flip the image horizontally (across y-axis)
img_horizontal = cv2.flip(image, 1)

print("Horizontal flip completed")
print("Original width:", image.shape[1])
print("Flipped width:", img_horizontal.shape[1])
Horizontal flip completed
Original width: 150
Flipped width: 150

Both Axes Flip (flipCode = -1)

In this example, we flip the input image across both axes (equivalent to 180° rotation) ?

import cv2
import numpy as np

# Create a sample image
image = np.zeros((100, 150, 3), dtype=np.uint8)
image[10:40, 20:80] = [255, 0, 0]  # Blue rectangle
image[60:90, 70:130] = [0, 255, 0]  # Green rectangle

# Flip the image across both axes
img_both = cv2.flip(image, -1)

print("Both axes flip completed")
print("This is equivalent to rotating the image by 180 degrees")
print("Shape remains the same:", img_both.shape)
Both axes flip completed
This is equivalent to rotating the image by 180 degrees
Shape remains the same: (100, 150, 3)

Complete Example with File Operations

Here's how you would typically use cv2.flip() with image files ?

import cv2

# Read input image
img = cv2.imread('input_image.jpg')

# Check if image is loaded
if img is None:
    print("Error: Could not load image")
    exit()

# Flip vertically
img_vertical = cv2.flip(img, 0)

# Flip horizontally  
img_horizontal = cv2.flip(img, 1)

# Flip both axes
img_both = cv2.flip(img, -1)

# Display images
cv2.imshow("Original", img)
cv2.imshow("Vertical Flip", img_vertical)
cv2.imshow("Horizontal Flip", img_horizontal)
cv2.imshow("Both Axes Flip", img_both)

# Wait for key press and close windows
cv2.waitKey(0)
cv2.destroyAllWindows()

Conclusion

Use cv2.flip() with flipCode 0 for vertical flip, 1 for horizontal flip, and -1 for both axes flip. The function preserves the original image dimensions and data type while creating a new flipped image.

Updated on: 2026-03-26T22:54:29+05:30

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements