【问题标题】:How to Index an Image (PIL) type array如何索引图像 (PIL) 类型的数组
【发布时间】:2022-01-06 11:53:21
【问题描述】:

假设我有两个图像连接,所以整个图像的形状是(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 数组?

【问题讨论】:

    标签: python image numpy python-imaging-library


    【解决方案1】:

    我会使用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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 2021-04-28
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2021-12-09
      相关资源
      最近更新 更多