eyal RnD
eyal RnD

Reputation: 45

Why loading a 2d array with PIL.Image.fromarray mode="L" change the image?

Why this code plot different images?

from PIL import Image
import numpy as np

x = (np.random.random((32,32))*255).astype(np.int16)

img1 = Image.fromarray(x, mode='L')
img2 = Image.fromarray(x)

plt.imshow(img1, cmap='gray')
plt.imshow(img2, cmap='gray')

see images:

enter image description here

enter image description here

Upvotes: 0

Views: 1312

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207445

PIL requires L mode images to be 8-bit, see here. So, if you pass in your 16-bit image, where every high byte is zero, every second pixel will be black.

Upvotes: 2

Related Questions