So I have an image in JPG format and I read this image as a tensor using two different ways:
- with
from torchvision.io import read_image
read_image(image_path)
and it gives me the tensor:
tensor([[[255, 255, 255, ..., 191, 191, 191],
[255, 255, 255, ..., 191, 191, 191],
[255, 255, 255, ..., 191, 191, 191],
...,
[255, 255, 255, ..., 191, 191, 191],
[255, 255, 255, ..., 191, 191, 191],
[255, 255, 255, ..., 191, 191, 191]],
[[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
...,
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255]],
[[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
...,
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255]],
[[129, 143, 143, ..., 255, 255, 255],
[143, 255, 254, ..., 255, 255, 255],
[143, 254, 255, ..., 255, 255, 255],
...,
[143, 255, 255, ..., 255, 255, 255],
[144, 254, 255, ..., 255, 255, 255],
[129, 144, 143, ..., 255, 255, 255]]], dtype=torch.uint8)
- when I use
from torchvision.transforms.functional import pil_to_tensor
img = PIL.Image.open(image_path)
pil_to_tensor(img)
it gives me the tensor
tensor([[[ 0, 0, 0, ..., 64, 64, 64],
[ 0, 0, 0, ..., 64, 64, 64],
[ 0, 0, 0, ..., 64, 64, 64],
...,
[ 0, 0, 0, ..., 64, 64, 64],
[ 0, 0, 0, ..., 64, 64, 64],
[ 0, 0, 0, ..., 64, 64, 64]],
[[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
...,
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0]],
[[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
...,
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0]],
[[126, 112, 112, ..., 0, 0, 0],
[112, 0, 1, ..., 0, 0, 0],
[112, 1, 0, ..., 0, 0, 0],
...,
[112, 0, 0, ..., 0, 0, 0],
[111, 1, 0, ..., 0, 0, 0],
[126, 111, 112, ..., 0, 0, 0]]], dtype=torch.uint8)
I get the correct and the original image when I use the first tensor not the second one. Also I observed that I can get the second tensor by subtracting each value in the first tensor from 255. Then my questions are:
- Why does the second way invert the original image?
- If I use the conversion formula
255 + second_tensor = first_tensor
, do I always get the right result?
Thanks!