Python NumPy flip array Function

Introduction

NumPy flip

The numpy.flip() function reverses the order of array elements along the specified axis, preserving the shape of the array.

Syntax:

numpy.flip(array, axis)

Parameters:

  • array: [array_like] Array to be input
  • axis: [integer] axis along which array is reversed.

Return: reversed array with shape preserved

NumPy fliplr

The numpy.fliplr() function Flip array (entries in each column) in left-right direction, shape preserved

Syntax:

numpy.fliplr(array)

Parameters:

  • array: [array_like] Array to be input

Return: Flipped array in the left-right direction.

NumPy flipud

The numpy.flipud() function flips the array(entries in each column) in up-down direction, shape preserved.

Syntax:

numpy.flipud(array)

Parameters:

  • array: [array_like] Array to be input

Return: Flipped array in the up-down direction.

Let’s have a look at some code snippets.

Code Examples

NumPy flip

Example 01:

# welcome to softhunt.net
# Python Program illustrating
# numpy.flip() method

import numpy as np

array = np.arange(27).reshape((3,3,3))
print("Original array : \n", array)

print("Flipped array : \n", np.flip(array, 0))

Output:

Original array : 
 [[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
Flipped array : 
 [[[18 19 20]
  [21 22 23]
  [24 25 26]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]]

NumPy fliplr

Example 01:

# welcome to softhunt.net
# Python Program illustrating
# numpy.flip() method

import numpy as np

array = np.arange(27).reshape((3,3,3))
print("Original array : \n", array)

print("Flipped array : \n", np.fliplr(array))

Output:

Original array : 
 [[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
Flipped array : 
 [[[ 6  7  8]
  [ 3  4  5]
  [ 0  1  2]]

 [[15 16 17]
  [12 13 14]
  [ 9 10 11]]

 [[24 25 26]
  [21 22 23]
  [18 19 20]]]

NumPy flipud

Example 01:

# welcome to softhunt.net
# Python Program illustrating
# numpy.flip() method

import numpy as np

array = np.arange(27).reshape((3,3,3))
print("Original array : \n", array)

print("Flipped array : \n", np.flipud(array))

Output:

Original array : 
 [[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[18 19 20]
  [21 22 23]
  [24 25 26]]]
Flipped array : 
 [[[18 19 20]
  [21 22 23]
  [24 25 26]]

 [[ 9 10 11]
  [12 13 14]
  [15 16 17]]

 [[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]]]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

How do I flip a NumPy array?

Flip an array vertically (axis=0). Flip an array horizontally (axis=1). flip(array, 0) is equivalent to flipud(array). flip(array, 1) is equivalent to fliplr(array).

How do I flip an image in NumPy?

Python’s OpenCV handles images as NumPy array ndarray. There are functions for rotating or flipping images (= ndarray) in OpenCV and NumPy, either of which can be use.

By reading the image as a NumPy array ndarray, various image processing can be perform using NumPy functions. By operating ndarray, you can get and set (change) pixel values, trim images, concatenate images, etc. Those who are familiar with NumPy can do various image processing without using libraries such as OpenCV.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. numpy.unique() – The NumPy unique Python Function

Leave a Comment