How to import and rotate an image using matplotlib ?

Published: May 02, 2021

Updated: May 15, 2023

Tags: Python; Scipy; Matplotlib; Image; Numpy;

DMCA.com Protection Status

Matplotlib allows you to import and rotate an image (see also how to rotate an image using python with pillow ? ). Examples

Importing an image using matploitlib

To do so, we first need to import an image using imread()

from matplotlib import image

data = image.imread("eiffel-tower.jpeg")

Note that:

print( type(data) )
print( data.shape )

returns

<class 'numpy.ndarray'>

and

(1280, 850, 3)

Plotting the image using matploitlib

One possible solution for plotting an imported image could be to utilize the imshow function:

plt.imshow(data)

plt.show()

 How to import (load) and rotate an image using matplotlib ?
How to import (load) and rotate an image using matplotlib ?

Rotating the image using scipy.ndimage

One possible solution for rotating an image is to use the ndimage module.

import scipy.ndimage as ndimage

angle = 45 # in degrees

new_data = ndimage.rotate(data, angle, reshape=True)

plt.imshow(new_data)

plt.savefig("rgb_image_rotation_scipy_matplotlib_02.png", bbox_inches='tight', dpi=100)

plt.show()

How to import (load) and rotate an image using matplotlib ?
How to import (load) and rotate an image using matplotlib ?

Note: "reshape=True" extend automatically the size of the image to display it entirely.

If reshape=False

angle = 45 # in degrees

new_data = ndimage.rotate(data, angle, reshape=False)

plt.imshow(new_data)

plt.savefig("rgb_image_rotation_scipy_matplotlib_03.png", bbox_inches='tight', dpi=100)

plt.show()

it returns

How to import (load) and rotate an image using matplotlib ?
How to import (load) and rotate an image using matplotlib ?

Create a loop to rotate the image

for angle in np.linspace(0,360,9):

    new_data = ndimage.rotate(data, angle, reshape=False)

    plt.imshow(new_data)

    plt.savefig("rgb_image_rotation_scipy_matplotlib_angle_{}.png".format(angle), bbox_inches='tight', dpi=100)

    plt.show()

returns:

How to import (load) and rotate an image using matplotlib ? How to import (load) and rotate an image using matplotlib ? How to import (load) and rotate an image using matplotlib ?
How to import (load) and rotate an image using matplotlib ? How to import (load) and rotate an image using matplotlib ? How to import (load) and rotate an image using matplotlib ?
How to import (load) and rotate an image using matplotlib ? How to import (load) and rotate an image using matplotlib ? How to import (load) and rotate an image using matplotlib ?
How to import (load) and rotate an image using matplotlib ?

Example of application: create an animated image (gif)

An example of application: (see also how to create an animated image (GIF) using python and Imagemagick)

import os

os.system("convert -delay 100 rgb_image_rotation_scipy_matplotlib_angle_* eiffel-tower.gif")

References

Links Site
scipy.ndimage.rotate docs.scipy.org
imread matplotlib.org
Image

of