【问题标题】:PIL: Image.fromarray(img.astype('uint8'), mode='RGB') returns grayscale imagePIL: Image.fromarray(img.astype('uint8'), mode='RGB') 返回灰度图像
【发布时间】:2019-05-27 17:57:33
【问题描述】:

我已将大小为 torch.Size([3, 28, 28]) 的 pytorch 张量转换为大小为 (28, 28, 3) 的 numpy 数组,这似乎没有任何问题。然后我尝试使用img = Image.fromarray(img.astype('uint8'), mode='RGB') 将其转换为PIL 图像,但返回的img 的尺寸是(28, 28),而我期望它是(28, 28, 3)(或(3, 28, 28))。我不明白为什么会这样。正如其他海报在线建议的那样,我确保转换为 uint8 并使用 RGB 模式,但是这些都没有帮助(也没有使用 np.ascontiguousarray)。

PIL 版本 1.1.7

# This code implements the __getitem__ function for a child class of datasets.MNIST in pytorch
# https://pytorch.org/docs/stable/_modules/torchvision/datasets/mnist.html#MNIST

img, label = self.data[index], self.targets[index]
assert img.shape == (3, 28, 28), \
       (f'[Before PIL] Incorrect image shape: expecting (3, 28, 28),'
        f'received {img.shape}')

print('Before reshape:', img.shape)    # torch.Size([3, 28, 28])
img = img.numpy().reshape(3, 28, 28)
img = np.stack([img[0,:,:], img[1,:,:], img[2,:,:]], axis=2)
print('After reshape:', img.shape)     # (28, 28, 3)

# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img.astype('uint8'), mode='RGB') # Returns 28 x 28 image

assert img.size == (3, 28, 28), \
       (f'[Before Transform] Incorrect image shape: expecting (3, 28, 28), '
        f'received {img.size}')

编辑:这是一个最小的例子。如果有任何帮助,我将把上面的内容留作上下文。

from PIL import Image
import numpy as np

img = np.random.randn(28, 28, 3)
img = Image.fromarray(img.astype('uint8'), mode='RGB') # Returns 28 x 28 image

assert img.size == (28, 28, 3), \
       (f'[Before Transform] Incorrect image shape: expecting (3, 28, 28), '
        f'received {img.size}')

AssertionError: [Before Transform] Incorrect image shape: expecting (3, 28, 28), received (28, 28)

【问题讨论】:

    标签: python numpy python-imaging-library


    【解决方案1】:

    我想你想要这个,其中 RGB 值的范围是 0..255 范围内的整数:

    import numpy as np
    from PIL import Image
    
    # Make random 28x28 RGB image
    img =np.random.randint(0,256,(28,28,3), dtype=np.uint8)
    
    # Convert to PIL Image
    pImg=Image.fromarray(img, mode='RGB')
    

    现在检查我们有什么:

    In [19]: pImg                                                                                       
    Out[19]: <PIL.Image.Image image mode=RGB size=28x28 at 0x120CE9CF8>
    

    然后保存:

    pImg.save('result.png')
    

    【讨论】:

    • 谢谢,这也是我的代码的一个问题,我没有意识到,但不是主要问题,它试图让图像尺寸包含颜色。实际上,由于您的回答,我能够解决它 - 我意识到一旦我看到您的 Out size=28x28 并且您的 pImg 是 RGB,Image.size 只返回宽度和高度(而不是#颜色通道)。所以也谢谢你!
    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2018-02-14
    • 2014-02-26
    相关资源
    最近更新 更多