Matplotlib将图像文件旋转X度 [英] Matplotlib rotate image file by X degrees

查看:1356
本文介绍了Matplotlib将图像文件旋转X度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何旋转图像文件并在matplotlib中绘制?



我知道我可以使用PIL打开并旋转它,但这似乎太过分了这个简单的函数,我可能找不到。



我在这里发现了这段代码,但似乎不起作用:

 来自matplotlib import pyplot,image,transforms 

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

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

范围内的度数(360):
pyplot.clf()
tr = transforms.Affine2D()。rotate_deg(degree)

ax.imshow(img,transform = tr)
fig.canvas.draw ()


解决方案

您可以使用中旋转 scipy.ndimage

 从进口SciPy的ndimage 
进口matplotlib.pyplot作为PLT

IMG = scipy.misc.lena()
#IMG = scipy.misc进口scipy.misc
.face()#lena不包含在scipy中0.19.1
plt.figure(figsize =(12,2))

范围内的度数(5):
plt.subplot(151 +度)
rotated_img = ndimage.rotate(IMG,度* 60)
plt.imshow(rotated_img,CMAP = plt.cm.gray)
plt.axis( 'off')

plt.show()

这会旋转图像围绕中心(参见。在这里,您可以使用 pygame.transform.rotate 旋转具有一定性能的图像(感谢基础SDL),并将旋转后的图像blitting到屏幕上。



试试这个(使用图片lena.jpg)获得平滑旋转的图像:

 进口pygame的

pygame.init()
屏幕= pygame.display.set_mode([400,400])
pygame.display.set_caption(旋转图像示例')
clock = pygame.time.Clock()

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

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

而degree< 360:
for pygame.event.get()中的事件:
if event.type == pygame.QUIT:
done = True

#rotate image
rot_img = pygame.transform.rotate(IMG,度)
img_rect = rot_img.get_rect(中心= img_rect.center)

#复制图像到屏幕
画面。 fill((0,0,0))
screen.blit(rot_img,img_rect)
pygame.display.flip()

clock.tick(60)
度+ = 1

pygame.quit()


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()

解决方案

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).

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()

这篇关于Matplotlib将图像文件旋转X度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆