python - 在 Matplotlib 中旋转图像

标签 python matplotlib image-rotation

我正在尝试在 Matplotlib 中绘制二维图像(从 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 文档和示例中弄清楚。

谢谢!

最佳答案

看看this代码:

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

关于python - 在 Matplotlib 中旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19167527/

相关文章:

python - PyQt 5 和 4k 屏幕

python - 如何使用互谱密度计算两个相关信号的相移

Java游戏旋转图像和轴

python - Sqlalchemy 真实总和

python - Paramiko 服务器 : Signalling the client that stdout is closed

python - 如何从一系列对象 pandas 中获取 1 列?

Python 在 PIL Image 对象上保存 matplotlib 图

python - 在 python 中绘制自定义函数

c++ - 如何将度数(角度)转换为面数

ios - 如何使图像始终显示正确的方向?