Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib rotate image file by X degrees

How can I rotate an image file and plot it in matplotlib?

I know I can open it using PIL and rotate it, but this seems way too much for this simple function, that I'm probably not finding.

I found this piece of code around here, but doesn't seems to work:

from matplotlib import pyplot, image, transforms

img = image.imread('filename.png')

pyplot.ion()
fig = pyplot.figure()
ax = fig.add_subplot(111)

for degree in range(360):
    pyplot.clf()
    tr = transforms.Affine2D().rotate_deg(degree)

    ax.imshow(img, transform=tr)
    fig.canvas.draw()
like image 699
f.rodrigues Avatar asked Jul 14 '15 08:07

f.rodrigues


4 Answers

You could use rotate from scipy.ndimage:

import scipy.misc
from scipy import ndimage
import matplotlib.pyplot as plt

img = scipy.misc.lena()  
# img = scipy.misc.face()  # lena is not included in scipy 0.19.1
plt.figure(figsize=(12, 2))

for degree in range(5):
    plt.subplot(151+degree)
    rotated_img = ndimage.rotate(img, degree*60)
    plt.imshow(rotated_img, cmap=plt.cm.gray)
    plt.axis('off')

plt.show()

This rotates the image around the center (see docs).

lena

Edit:

I you want some kind of animation (I don't how you're going to use the rotating image, so I can only speculate), maybe you're better off using some kind of game/graphics library, e.g. Pygame. Here you can rotate an image with some performance (thanks to the underlying SDL) by using pygame.transform.rotate and blitting that rotated image onto the screen.

Try this (using a picture lena.jpg) to get a smoothly rotating image:

import pygame

pygame.init()
screen = pygame.display.set_mode([400, 400])
pygame.display.set_caption('Rotating image example')
clock = pygame.time.Clock()

img = pygame.image.load('lena.jpg').convert()

img_rect = img.get_rect(center = screen.get_rect().center)
degree = 0

while degree < 360:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # rotate image
    rot_img = pygame.transform.rotate(img, degree)
    img_rect = rot_img.get_rect(center = img_rect.center)

    # copy image to screen
    screen.fill((0, 0, 0))
    screen.blit(rot_img, img_rect)
    pygame.display.flip()

    clock.tick(60)
    degree += 1

pygame.quit()
like image 25
adrianus Avatar answered Nov 08 '22 11:11

adrianus


It seems matplotlib has moved on since this question was asked, and it can now natively support affine transformations of images without the user needing to fall back to low-level image manipulation routines.

This is now documented in the form of an example in https://matplotlib.org/gallery/images_contours_and_fields/affine_image.html.

Essentially, this requires specifying the appropriate transform keyword argument. The code provided in the original question would be updated as follows:

import matplotlib.pyplot as plt
from matplotlib import transforms

img = plt.imread('filename.png')

fig = plt.figure()
ax = fig.add_subplot(111)

tr = transforms.Affine2D().rotate_deg(rotation_in_degrees)

ax.imshow(img, transform=tr + ax.transData)
plt.show()
like image 57
pelson Avatar answered Nov 08 '22 12:11

pelson


Another option might be to use numpy's rot90 function. The idea is to convert the image into a numpy array and then rotate the array the number of times needed. Here's an example:

import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import numpy as np

with cbook.get_sample_data('ada.png') as image_file:
    image = plt.imread(image_file)
image_arr = np.array(image)

#plt.show() To view the image

for _ in range(3):#3 to turn the image 90 degrees three times = 270 degrees
    image_arr = np.rot90(image_arr)

plt.imshow(image_arr, aspect="auto", extent=(-14,-4,-2,40), zorder = 2, interpolation="nearest")

plt.show()

However, this is only limited to rotating an image by right angles (multiples of 90).

like image 26
Abhishek Avatar answered Nov 08 '22 12:11

Abhishek


you could use the following code, which I found here:

How can I rotate a matplotlib plot through 90 degrees?

import matplotlib.pyplot as plt
import scipy

img = scipy.misc.lena()
tr = scipy.ndimage.rotate(img, 45)
plt.imshow(tr)
like image 3
Achim Avatar answered Nov 08 '22 13:11

Achim