DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

ToPILImage in PyTorch (3)

Buy Me a Coffee

*Memos:

ToPILImage() can convert an Image([..., C, H, W]), tensor or ndarray to a PIL(Pillow library) image([H, W, C]) and doesn't scale its values to [0.0, 1.0] as shown below. *It's about mode argument (2):

from torchvision.datasets import OxfordIIITPet from torchvision.transforms.v2 import ToPILImage import numpy as np tp = ToPILImage() tp = ToPILImage(mode="I") tp((np.array([[[0]]]), 0)) # int32 # (<PIL.Image.Image image mode=I size=1x1>, 0)  tp = ToPILImage(mode="LA") tp((np.array([[[0, 1]]]), 0)) tp((np.array([[[0, 1]]], dtype=np.int64), 0)) # (<PIL.Image.Image image mode=LA size=1x1>, 0)  tp = ToPILImage(mode="RGB") tp((np.array([[[0, 1, 2]]]), 0)) tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0)) # (<PIL.Image.Image image mode=RGB size=1x1>, 0)  tp = ToPILImage(mode="YCbCr") tp((np.array([[[0, 1, 2]]]), 0)) tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0)) # (<PIL.Image.Image image mode=YCbCr size=1x1>, 0)  tp = ToPILImage(mode="HSV") tp((np.array([[[0, 1, 2]]]), 0)) tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0)) # (<PIL.Image.Image image mode=HSV size=1x1>, 0)  tp = ToPILImage(mode="RGBA") tp((np.array([[[0, 1, 2, 3]]]), 0)) tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0)) # (<PIL.Image.Image image mode=RGBA size=1x1>, 0)  tp = ToPILImage(mode="CMYK") tp((np.array([[[0, 1, 2, 3]]]), 0)) tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0)) # (<PIL.Image.Image image mode=CMYK size=1x1>, 0)  tp = ToPILImage(mode="RGBX") tp((np.array([[[0, 1, 2, 3]]]), 0)) tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0)) # (<PIL.Image.Image image mode=RGBX size=1x1>, 0)  tp = ToPILImage() tp = ToPILImage(mode="L") tp((np.array([[[0.]]]), 0)) # float64 tp((np.array([[[0.]]], dtype=np.float32), 0)) # (<PIL.Image.Image image mode=L size=1x1>, 0)  tp = ToPILImage() tp = ToPILImage(mode="LA") tp((np.array([[[0., 1.]]]), 0)) tp((np.array([[[0., 1.]]], dtype=np.float32), 0)) # (<PIL.Image.Image image mode=LA size=1x1>, 0)  tp = ToPILImage() tp = ToPILImage(mode="RGB") tp((np.array([[[0., 1., 2.]]]), 0)) tp((np.array([[[0., 1., 2.]]], dtype=np.float32), 0)) # (<PIL.Image.Image image mode=RGB size=1x1>, 0)  tp = ToPILImage(mode="YCbCr") tp((np.array([[[0., 1., 2.]]]), 0)) tp((np.array([[[0., 1., 2.]]], dtype=np.float32), 0)) # (<PIL.Image.Image image mode=YCbCr size=1x1>, 0)  tp = ToPILImage(mode="HSV") tp((np.array([[[0., 1., 2.]]]), 0)) tp((np.array([[[0., 1., 2.]]], dtype=np.float32), 0)) # (<PIL.Image.Image image mode=HSV size=1x1>, 0)  tp = ToPILImage() tp = ToPILImage(mode="RGBA") tp((np.array([[[0., 1., 2., 3.]]]), 0)) tp((np.array([[[0., 1., 2., 3.]]], dtype=np.float32), 0)) # (<PIL.Image.Image image mode=RGBA size=1x1>, 0)  tp = ToPILImage(mode="CMYK") tp((np.array([[[0., 1., 2., 3.]]]), 0)) tp((np.array([[[0., 1., 2., 3.]]], dtype=np.float32), 0)) # (<PIL.Image.Image image mode=CMYK size=1x1>, 0)  tp = ToPILImage(mode="RGBX") tp((np.array([[[0., 1., 2., 3.]]]), 0)) tp((np.array([[[0., 1., 2., 3.]]], dtype=np.float32), 0)) # (<PIL.Image.Image image mode=RGBX size=1x1>, 0)  tp = ToPILImage(mode="LA") tp((np.array([[[0.+0.j, 1.+0.j]]]), 0)) # complex128 tp((np.array([[[0.+0.j, 1.+0.j]]], dtype=np.complex64), 0)) # (<PIL.Image.Image image mode=LA size=1x1>, 0)  tp = ToPILImage(mode="RGB") tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j]]]), 0)) tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j]]], dtype=np.complex64), 0)) # (<PIL.Image.Image image mode=RGB size=1x1>, 0)  tp = ToPILImage(mode="YCbCr") tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j]]]), 0)) tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j]]], dtype=np.complex64), 0)) # (<PIL.Image.Image image mode=YCbCr size=1x1>, 0)  tp = ToPILImage(mode="HSV") tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j]]]), 0)) tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j]]], dtype=np.complex64), 0)) # (<PIL.Image.Image image mode=HSV size=1x1>, 0)  tp = ToPILImage(mode="RGBA") tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]]), 0)) tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]], dtype=np.complex64), 0)) # (<PIL.Image.Image image mode=RGBA size=1x1>, 0)  tp = ToPILImage(mode="CMYK") tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]]), 0)) tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]], dtype=np.complex64), 0)) # (<PIL.Image.Image image mode=CMYK size=1x1>, 0)  tp = ToPILImage(mode="RGBX") tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]]), 0)) tp((np.array([[[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]], dtype=np.complex64), 0)) # (<PIL.Image.Image image mode=RGBX size=1x1>, 0)  tp = ToPILImage(mode="LA") tp((np.array([[[True, False]]]), 0)) # bool # (<PIL.Image.Image image mode=LA size=1x1>, 0)  tp = ToPILImage(mode="RGB") tp((np.array([[[True, False, True]]]), 0)) # (<PIL.Image.Image image mode=RGB size=1x1>, 0)  tp = ToPILImage(mode="YCbCr") tp((np.array([[[True, False, True]]]), 0)) # (<PIL.Image.Image image mode=YCbCr size=1x1>, 0)  tp = ToPILImage(mode="HSV") tp((np.array([[[True, False, True]]]), 0)) # (<PIL.Image.Image image mode=HSV size=1x1>, 0)  tp = ToPILImage(mode="RGBA") tp((np.array([[[True, False, True, False]]]), 0)) # (<PIL.Image.Image image mode=RGBA size=1x1>, 0)  tp = ToPILImage(mode="CMYK") tp((np.array([[[True, False, True, False]]]), 0)) # (<PIL.Image.Image image mode=CMYK size=1x1>, 0)  tp = ToPILImage(mode="RGBX") tp((np.array([[[True, False, True, False]]]), 0)) # (<PIL.Image.Image image mode=RGBX size=1x1>, 0) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)