0

im working with a hamamatsu camera, I get a NumPy array and I want to save the array like an image, I can do it to a TIF image but i don't know how to convert the TIF image or the array to get a correct jpg image, I have this code:

img = Image.fromarray(self.val_fin)

    if int(self.vTIFF.get()) == 1:
        imgTIFF = img.convert('I')
        img.save('name1.tiff')

    if int(self.vJPG.get()) == 1:
        imgJPG = img.convert('RGB')
        imgJPG.save('name2.jpg')

Where val_fin is a 32bit array whose negative values ​​have been changed to 0, the result of the jpg image is a black image. Thanks.

  • You have a hamamatsu camera... whose model you don't specify. You receive a Numpy array... presumably from the camera... whose dimensions, `dtype`, `minima`, `maxima` and number of channels you don't specify... and you'd like to make a JPEG. Please try and be clearer so we can help you better. Thank you. – Mark Setchell Nov 17 '21 at 23:31
  • reduce image to 16bit , try again if fails try converting to 8bit deepth try again – pippo1980 Nov 18 '21 at 08:26
  • @MarkSetchell Do you have by any chance a sample hamamatsu camera file, I've found this https://www.hamamatsu.com/sp/sys/en/camera_simulator/index.html but downloads a png !! – pippo1980 Nov 18 '21 at 21:51

1 Answers1

0

using tiff 32bites float image:

tiff 32bites float

I can run this code:

from PIL import Image

import numpy as np


def normalize8(I):
  mn = I.min()
  mx = I.max()

  mx -= mn

  I = ((I - mn)/mx) * 255.0
  return np.round(I).astype(np.uint8)

img1 = Image.open('test_fl.tif', 'r')

arr = np.asarray(img1)

print(arr.size, arr.shape, arr.ndim , arr.dtype)


img = Image.fromarray(arr, mode='F')
print(img.size, img.format, img.mode)
img.save('test_saved.tif')

# doesnt work
# imgTIFF = img.convert(mode='I')
# imgTIFF.save('name1.tif')
# img2 = Image.open('name1.tif', 'r')
# print(img2.size, img2.format, img2.mode)

imgTIFF = Image.fromarray(normalize8(arr))
imgTIFF.save('name1.tif')
img2 = Image.open('name1.tif', 'r')
print(img2.size, img2.format, img2.mode)



imgJPG = imgTIFF.convert('RGB')
imgJPG.save('name2.jpg')

img3 = Image.open('name2.jpg')
print(img3.size, img3.format, img3.mode)
img3.show()

print(img3.getpixel((0,0)))

taken from How should I convert a float32 image to an uint8 image?

the imgTIFF = img.convert(mode='I')

trying to convert tiff 32float to 32int gives a black Image to me too

pippo1980
  • 2,181
  • 3
  • 14
  • 30