繁体   English   中英

在Matplotlib中旋转图像

[英]Rotate an image in Matplotlib

我正在尝试在Matplotlib中绘制一个2D图像(从png导入)并以任意角度旋转它。 我想创建一个简单的动画来显示一个对象随时间的旋转,但是现在我只想尝试旋转图像。 我在以下代码上尝试了几种变体但没有成功:

import matplotlib.pyplot as plt
import matplotlib.transforms as tr
import matplotlib.cbook as cbook

image_file = cbook.get_sample_data('ada.png')
image = plt.imread(image_file)

imAx = plt.imshow(image)
rot = tr.Affine2D().rotate_deg(30)
imAx.set_transform(imAx.get_transform()+rot)

plt.axis('off') # clear x- and y-axes
plt.show()

我确定我错过了一些东西,但我无法从matplotlib文档和示例中找到它。

谢谢!

看看这段代码:

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import numpy as np

lena = scipy.misc.lena()
lx, ly = lena.shape
# Copping
crop_lena = lena[lx/4:-lx/4, ly/4:-ly/4]
# up <-> down flip
flip_ud_lena = np.flipud(lena)
# rotation
rotate_lena = ndimage.rotate(lena, 45)
rotate_lena_noreshape = ndimage.rotate(lena, 45, reshape=False)

plt.figure(figsize=(12.5, 2.5))


plt.subplot(151)
plt.imshow(lena, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(152)
plt.imshow(crop_lena, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(153)
plt.imshow(flip_ud_lena, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(154)
plt.imshow(rotate_lena, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(155)
plt.imshow(rotate_lena_noreshape, cmap=plt.cm.gray)
plt.axis('off')

plt.subplots_adjust(wspace=0.02, hspace=0.3, top=1, bottom=0.1, left=0,
                    right=1)

plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM