假設我有兩個影像連接,所以整個影像的形狀(256,512)是這樣的:[X|X]
我希望能夠做到這一點:
from PIL import Image
both_image = Image.open(path)
img_left = both_image[:,:256]
img_right = both_image[:,256:]
但 *** TypeError: 'PngImageFile' object is not subscriptable
為了解決它,我在寫:both_image = np.array(Image.open(path))并Image.fromarray()在最后使用
有沒有更好的方法可以做到這一點,而無需來回從 Image 型別到 numpy 陣列?
uj5u.com熱心網友回復:
我會使用Image.crop和裁剪both_image兩次。
來自 Pllow 檔案:
Image.crop(框=無)從該影像回傳一個矩形區域。該框是一個 4 元組,定義了左、上、右和下像素坐標。
使用相同的設定:
from PIL import Image
both_image = Image.open(path)
裁剪左側影像,從 (0,0) 到 (256,256):
img_left = both_image.crop((0,0,256,256))
裁剪右圖,從 (256,0) 到 (512,256):
img_right = both_image.crop((256,0,512,256))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/405427.html
標籤:
上一篇:如何向此影像添加延遲加載?
