Saving normalized image seems to convert it back to not-normalized

Dear @John6666

I saw in your dataset that your images stays normalized, but when I save it with matplotlib it converts back to 0-255 images (black and white). Is there a method to make it stays 0 and 1 as jpeg?

Here’s the proof of normalization before opening it back with PIL

after saving:


image_normalized = im.open("D:\semester_12\Data_Irredeano\\img_0.jpg")

npimage = np.array(image_normalized)

print(npimage)

output:


[[[  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]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]]

On loop

normalization code

imar = np.asarray(water_mask) / 255
plt.imsave('D:/'+'img_'+str(a)+'.jpg', imar, cmap="gray")  
                

Output
[[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 1. 1. 1.]
 [0. 0. 0. ... 1. 1. 1.]
 [0. 0. 0. ... 1. 1. 1.]]

Using images from dataset in huggingface

Or should I add the .convert(‘L’) as PIL.Image after converting it to np.array?
Thanks in advance

1 Like

Or should I add the .convert(‘L’) as PIL.Image after converting it to np.array?

That’s probably more reliable. Strictly speaking, using only 0s and 1s is not a grayscale (L in PIL) but a binary (monochrome) format. Since you need a grayscale image with monochrome information in this special situation, it’s better to convert it yourself each time.:sweat_smile: If you do it automatically, it’s likely to be converted back when you process it.

JPEG is a format that is supposed to be suitable for color images, based on the compression principle that assumes human vision. It’s a format that is not suitable for computer masks. I think that many people save images for masking purposes in PNG format. WEBP is also fine, but PNG is easier to use on Windows.

I see, yes yes I mentioned it wrong it’s binary instead of grayscale after normalized. But you can upload to huggingface as binary format, do you convert then immediately uploaded it without saving it into jpeg first?

Can you just name it .png to make it png in matplotlib? Because I did it once but it become jpeg anyway.

Btw thanks for helping me this far.

1 Like

yw!

Can you just name it .png to make it png in matplotlib? Because I did it once but it become jpeg anyway.

I see. It’s a matplotlib specification!
If that’s the case, it’s probably quicker to save it as a jpeg, open it with PIL and numpy, and fix it.