1
votes

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")
1
Check imarray.shape before and after the multiply, per stackoverflow.com/questions/7700193/… - lnmx
>>> 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
Maybe a data type/range issue? Check imarray.dtype before/after and try imarray = numpy.uint8(imarray) before fromarray. - lnmx
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

1 Answers

1
votes

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( ... ).