Saving normalized image seems to convert it back to not-normalized
Dear@John6666I saw in yourdatasetthat your images stays normalized, but when I save it withmatplotlibit 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 withPILafter 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 loopnormalization codeimar = 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 huggingfaceOr should I add the .convert(‘L’) asPIL.Imageafter converting it to np.array?Thanks in advance
2025-01-08T05:27:01Z
Or should I add the .convert(‘L’) asPIL.Imageafter 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.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.
2025-01-08T13:26:13Z
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.
2025-01-08T14:35:13Z
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.