簡體   English   中英

Matplotlib 將圖像文件旋轉 X 度

[英]Matplotlib rotate image file by X degrees

如何旋轉圖像文件並將其繪制在 matplotlib 中?

我知道我可以使用 PIL 打開它並旋轉它,但這對於這個簡單的功能來說似乎太多了,我可能找不到。

我在這里找到了這段代碼,但似乎不起作用:

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

您可以使用來自scipy.ndimage rotate

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

這會圍繞中心旋轉圖像(請參閱文檔)。

莉娜

編輯:

我想要某種動畫(我不知道你將如何使用旋轉圖像,所以我只能推測),也許你最好使用某種游戲/圖形庫,例如Pygame 在這里,您可以通過使用pygame.transform.rotate並將旋轉后的圖像塊傳輸到屏幕上來旋轉具有一定性能的圖像(感謝底層 SDL)。

試試這個(使用圖片lena.jpg)以獲得平滑旋轉的圖像:

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 已經取得了進展,現在它可以原生支持圖像的仿射變換,而無需用戶退回到低級圖像處理例程。

這現在以https://matplotlib.org/gallery/images_contours_and_fields/affine_image.html中的示例形式記錄。

本質上,這需要指定適當的轉換關鍵字參數。 原始問題中提供的代碼將更新如下:

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

你可以使用以下代碼,我在這里找到:

如何將 matplotlib 圖旋轉 90 度?

import matplotlib.pyplot as plt
import scipy

img = scipy.misc.lena()
tr = scipy.ndimage.rotate(img, 45)
plt.imshow(tr)

我發現以下解決方案非常直觀。 它使用 Pillow 庫讀取圖像,然后,我們可以使用內置的旋轉功能來旋轉圖像。 圖像以逆時針方式旋轉並存儲在新變量中。 最后,我們可以使用 matplotlib 創建和顯示繪圖。

from PIL import Image
import matplotlib.pyplot as plt

im = Image.open(path-to-file)
im = im.rotate(90) # Rotates counter clock-wise.
implot = plt.imshow(im)
plt.show()

另一種選擇可能是使用numpyrot90函數。 這個想法是將圖像轉換為一個 numpy 數組,然后將數組旋轉所需的次數 下面是一個例子:

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

但是,這僅限於將圖像旋轉直角(90 的倍數)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM