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
# (, 0)

tp = ToPILImage(mode="LA")

tp((np.array([[[0, 1]]]), 0))
tp((np.array([[[0, 1]]], dtype=np.int64), 0))
# (, 0)

tp = ToPILImage(mode="RGB")

tp((np.array([[[0, 1, 2]]]), 0))
tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0))
# (, 0)

tp = ToPILImage(mode="YCbCr")

tp((np.array([[[0, 1, 2]]]), 0))
tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0))
# (, 0)

tp = ToPILImage(mode="HSV")

tp((np.array([[[0, 1, 2]]]), 0))
tp((np.array([[[0, 1, 2]]], dtype=np.int64), 0))
# (, 0)

tp = ToPILImage(mode="RGBA")

tp((np.array([[[0, 1, 2, 3]]]), 0))
tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0))
# (, 0)

tp = ToPILImage(mode="CMYK")

tp((np.array([[[0, 1, 2, 3]]]), 0))
tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0))
# (, 0)

tp = ToPILImage(mode="RGBX")

tp((np.array([[[0, 1, 2, 3]]]), 0))
tp((np.array([[[0, 1, 2, 3]]], dtype=np.int64), 0))
# (, 0)

tp = ToPILImage()
tp = ToPILImage(mode="L")

tp((np.array([[[0.]]]), 0)) # float64
tp((np.array([[[0.]]], dtype=np.float32), 0))
# (, 0)

tp = ToPILImage()
tp = ToPILImage(mode="LA")

tp((np.array([[[0., 1.]]]), 0))
tp((np.array([[[0., 1.]]], dtype=np.float32), 0))
# (, 0)

tp = ToPILImage()
tp = ToPILImage(mode="RGB")

tp((np.array([[[0., 1., 2.]]]), 0))
tp((np.array([[[0., 1., 2.]]], dtype=np.float32), 0))
# (, 0)

tp = ToPILImage(mode="YCbCr")

tp((np.array([[[0., 1., 2.]]]), 0))
tp((np.array([[[0., 1., 2.]]], dtype=np.float32), 0))
# (, 0)

tp = ToPILImage(mode="HSV")

tp((np.array([[[0., 1., 2.]]]), 0))
tp((np.array([[[0., 1., 2.]]], dtype=np.float32), 0))
# (, 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))
# (, 0)

tp = ToPILImage(mode="CMYK")

tp((np.array([[[0., 1., 2., 3.]]]), 0))
tp((np.array([[[0., 1., 2., 3.]]], dtype=np.float32), 0))
# (, 0)

tp = ToPILImage(mode="RGBX")

tp((np.array([[[0., 1., 2., 3.]]]), 0))
tp((np.array([[[0., 1., 2., 3.]]], dtype=np.float32), 0))
# (, 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))
# (, 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))
# (, 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))
# (, 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))
# (, 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))
# (, 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))
# (, 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))
# (, 0)

tp = ToPILImage(mode="LA")

tp((np.array([[[True, False]]]), 0)) # bool
# (, 0)

tp = ToPILImage(mode="RGB")

tp((np.array([[[True, False, True]]]), 0))
# (, 0)

tp = ToPILImage(mode="YCbCr")

tp((np.array([[[True, False, True]]]), 0))
# (, 0)

tp = ToPILImage(mode="HSV")

tp((np.array([[[True, False, True]]]), 0))
# (, 0)

tp = ToPILImage(mode="RGBA")

tp((np.array([[[True, False, True, False]]]), 0))
# (, 0)

tp = ToPILImage(mode="CMYK")

tp((np.array([[[True, False, True, False]]]), 0))
# (, 0)

tp = ToPILImage(mode="RGBX")

tp((np.array([[[True, False, True, False]]]), 0))
# (, 0)