Vision: F.to_pil_image for two dimensional arrays

Created on 4 Jan 2018  路  3Comments  路  Source: pytorch/vision

Would it be possible to have to_pil_image work for 2D arrays and convert them to an L PIL Images?

Currently, it converts 3 dimensional arrays to RGB if they have 3 channels and to L if they have one channel.

However, like in the code below, to_pil_image complains if it is called on a 2D array.

Would it be simpler if 2D arrays are assumed to be single channel images so that to_pil_image converts them to L PIL images ? Thanks.

ipdb> import torchvision.transforms.functional as FT

ipdb> FT.to_pil_image(_points[:,:,0]) 
*** IndexError: tuple index out of range

ipdb> FT.to_pil_image(_points[:,:,:1])
<PIL.Image.Image image mode=L size=640x480 at 0x7FD4947341D0>

Most helpful comment

@alykhantejani may be torchvision should report something else than IndexError: tuple index out of range ?

--> 108     if npimg.shape[2] == 1:
    109         expected_mode = None
    110         npimg = npimg[:, :, 0]

for example, check npimg.ndim == 3 here and raise about what kind of input we need.

All 3 comments

@IssamLaradji I guess this could be made possible, but when you convert back to a tensor you will get a [1xHxW] tensor as the generic fromat in torch is batch x channels x height x width for images.

I see! I guess it's better to keep it consistent this way. Thanks ! :)

@alykhantejani may be torchvision should report something else than IndexError: tuple index out of range ?

--> 108     if npimg.shape[2] == 1:
    109         expected_mode = None
    110         npimg = npimg[:, :, 0]

for example, check npimg.ndim == 3 here and raise about what kind of input we need.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Abolfazl-Mehranian picture Abolfazl-Mehranian  路  3Comments

alpha-gradient picture alpha-gradient  路  3Comments

Linardos picture Linardos  路  4Comments

ibtingzon picture ibtingzon  路  3Comments

xuanqing94 picture xuanqing94  路  3Comments