1

I am trying to reduce my cannon astro images by taking off the flatframe, which works. But it leaves all the values very low (so almost black picture), which is why I also want to multiply it with the average. However this gives me an error. (while without multiplication it works.)

Does anybody know why?

Traceback (most recent call last): File "D:\astro\10-12\moon\fits\red.py", line 16, in img = Image.fromarray(imarray) File "C:\Python27\lib\site-packages\PIL\Image.py", line 1886, in fromarray raise TypeError("Cannot handle this data type") TypeError: Cannot handle this data type

Here is my code

import Image
import numpy as np

im = Image.open('8bit/DPP_0001.TIF')
flat = Image.open('8bit/flat2.TIF')
#im.show()


imarray = np.array(im)
flatarray = np.array(flat)

avg = np.average(imarray)
imarray = (imarray/flatarray)*avg

img = Image.fromarray(imarray)

img.save("done/aap.png","png")
Coolcrab
  • 2,655
  • 9
  • 39
  • 59
  • Check `imarray.shape` before and after the multiply, per http://stackoverflow.com/questions/7700193/pil-cannot-handle-this-data-type – lnmx Dec 12 '13 at 20:39
  • `>>> imarray = np.array(im) >>> imarray.shape (2848, 4272, 3) >>> avg = np.average(imarray) >>> imarray = (imarray/flatarray)*avg >>> imarray.shape (2848, 4272, 3)` The shape did not change with me oddly – Coolcrab Dec 12 '13 at 20:51
  • 1
    Maybe a data type/range issue? Check `imarray.dtype` before/after and try `imarray = numpy.uint8(imarray)` before `fromarray`. – lnmx Dec 12 '13 at 20:58
  • Ah it was a float instead of a unit8! `imarray = np.uint8((imarray/flatarray)*avg)` Worked :D Post it and I'll upvote you :P – Coolcrab Dec 12 '13 at 21:05

1 Answers1

1

PIL's Image.fromarray() supports a limited range of input type/channel combinations (see PIL/Image.py, member _fromarray_typemap).

The original imarray loaded from the TIF file had 3 channels of 8-bit integer values (bytes).

In your case, the average value of the image yielded a float value, and when that was multiplied with the image data, it produced float values for all of the pixels.

For fromarray to work, you need to coerce the pixel values back to byte values with np.uint8( ... ).

lnmx
  • 10,846
  • 3
  • 40
  • 36