Probelm showing RGB image using PIL

Viewed 156

In my code, I am creating an image as a RGB array (224 * 224 * 3).

This is how my image representation looks like:

[[[0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  ...,  [0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.84066298]],, [[0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  ...,  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.84066298]],, [[0.44861878 0.65767956 0.95801105],  [0.44861878 0.64972376 0.95801105],  [0.44861878 0.64972376 0.95801105],  ...,  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.84066298]],, ...,, [[0.44861878 0.45082873 0.83270718],  [0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.82475138],  ...,  [0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.82475138]],, [[0.44861878 0.45082873 0.82475138],  [0.44861878 0.45082873 0.82475138],...

now, when I use:

plt.imshow(image)
plt.show()

I am getting this image:

enter image description here

when I use :

  image = np.asarray(image * 255, dtype=np.int)
  PIL.Image.fromarray(image, "RGB").show()

I am getting :

enter image description here

Why can't I show the image using PIL?

1 Answers

Your problem seems to be that you create a NumPy array of type np.int (64-bit signed integer) and PIL expects np.uint8 (8-bit unsigned integer). Changing image = np.asarray(image * 255, dtype=np.int) to image = np.asarray(image * 255, dtype=np.uint8) should fix your problem.

Related