PIL python library loads only 0 but still displays image
Question:
import numpy as np
from PIL import Image
image_path = f"D:datasetsdataset_game_objects\1.jpg"
image = Image.open(image_path)
image_array = np.array(image)
image.show()
I am running following simple code for testing if path to my image is correct but I have encouter rather weird error my image opens but when I transform it into array i get following output
[[[0 4 0]
[0 4 0]
[0 4 0]
...
[0 0 0]
[1 0 0]
[1 0 0]]
[[0 4 0]
[0 4 0]
[0 4 0]
...
[0 0 0]
[1 0 0]
[1 0 0]]
[[1 3 0]
[1 3 0]
[1 3 0]
...
[0 0 0]
[1 0 0]
[1 0 0]]
...
[[0 0 0]
[0 0 0]
[0 0 0]
...
[2 1 0]
[3 2 0]
[4 3 1]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[1 0 0]
[1 0 0]
[2 1 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[1 0 0]
[1 0 0]
[1 0 0]]]
so I tried PilToTensor() as transformation
tensor([[[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.]],
[[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.]]])
So this is rather weird behavior the the pictures are all jpgs
Answers:
Have you already taken into account that torchvision.transforms.PILToTensor
method changes dimensionality oder. Quoting from the documentation:
Converts a PIL Image (H x W x C) to a Tensor of shape (C x H x W).
Ref: https://pytorch.org/vision/stable/generated/torchvision.transforms.PILToTensor.html
If yes , could you please share the pic as well, I can give it a go.
P.S: Could’ve simply commented this but don’t have enough points in my profile to do that yet
I just needed to change my transformation function
import numpy as np
from PIL import Image
image_path = f"D:datasetsdataset_game_objects\1.jpg"
image = Image.open(image_path)
image_array = np.array(image)
image.show()
I am running following simple code for testing if path to my image is correct but I have encouter rather weird error my image opens but when I transform it into array i get following output
[[[0 4 0]
[0 4 0]
[0 4 0]
...
[0 0 0]
[1 0 0]
[1 0 0]]
[[0 4 0]
[0 4 0]
[0 4 0]
...
[0 0 0]
[1 0 0]
[1 0 0]]
[[1 3 0]
[1 3 0]
[1 3 0]
...
[0 0 0]
[1 0 0]
[1 0 0]]
...
[[0 0 0]
[0 0 0]
[0 0 0]
...
[2 1 0]
[3 2 0]
[4 3 1]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[1 0 0]
[1 0 0]
[2 1 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[1 0 0]
[1 0 0]
[1 0 0]]]
so I tried PilToTensor() as transformation
tensor([[[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.]],
[[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.]]])
So this is rather weird behavior the the pictures are all jpgs
Have you already taken into account that torchvision.transforms.PILToTensor
method changes dimensionality oder. Quoting from the documentation:
Converts a PIL Image (H x W x C) to a Tensor of shape (C x H x W).
Ref: https://pytorch.org/vision/stable/generated/torchvision.transforms.PILToTensor.html
If yes , could you please share the pic as well, I can give it a go.
P.S: Could’ve simply commented this but don’t have enough points in my profile to do that yet
I just needed to change my transformation function