-2

I have a numpy array of size (512,512) with maximum intensity of 3071 and minimum intensity of -1024. I want to preserve the intensity while converting it to (512,512,1). I tried Image.Open(fromarray().convert('L')) but it converts the intensities to 0 and 255.

Please guide me what can be the best way to achieve the above goal. Original data is grayscale.

azro
  • 53,056
  • 7
  • 34
  • 70
  • Exactly what grayscale image *format* are you trying to convert to? – martineau Aug 02 '21 at 16:04
  • so you're just trying to add a dimension shape wise? https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html – Cptmaxon Aug 02 '21 at 16:32
  • not an expert here tried to answer compressing your 4095 range into 8bit grayscale 0 to 255 then realized you want to keep your min max values. Can use 16bit grayscale image but keeping your range everithing will look black upon img.show(). as per creating your image mode in pillow think we need big guns – pippo1980 Aug 02 '21 at 18:07
  • you could create an array from o to 65535 (16bit) populated only by your 4095 different values and fill a 16bit array representing your pic with those values but I think that won't answer your problem: scale = np.linspace(0,65535, 4095).astype(int) then replace values in your array with values from scale – pippo1980 Aug 02 '21 at 18:26
  • try to search for 12bit image mode see here https://stackoverflow.com/questions/63310083/dynamic-range-bit-depth-in-pils-fromarray-function – pippo1980 Aug 02 '21 at 19:46

1 Answers1

0

try using:

img = Image.fromarray(arr.astype(np.uint16) * 2**4)

explanation somehow here:

Dynamic range (bit depth) in PIL's fromarray() function

pippo1980
  • 2,181
  • 3
  • 14
  • 30