<p>I haven't done much astronomical image processing before, but as this question is unanswered I'll give it a shot - hopefully to some avail. If the problem is more specific, a code sample/image sample would probably be useful for further diagnosis, but otherwise <a href="http://docs.astropy.org/en/stable/generated/examples/io/split-jpeg-to-fits.html" rel="nofollow noreferrer">this example</a> may help. It discusses the process of writing a 3-channel image to separate FITS images. I would try this first, and check if this output is also garbage - it might just be an issue with how you're using the HDUList.writeto() function. The relevant code is pasted below (with a couple of edits, as the example uses the Pillow Image class at first):</p>

<h3>Split the three channels (RGB):</h3>

<pre><code>r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
</code></pre>

<h3>Write out the channels as separate FITS images</h3>

<pre><code>red = fits.PrimaryHDU(data=r)
red.writeto('red.fits')

green = fits.PrimaryHDU(data=g)
green.writeto('green.fits')

blue = fits.PrimaryHDU(data=b)
blue.writeto('blue.fits')
</code></pre>

<p>Also out of curiosity - why is your "numpy RGB array" storing values between 0 and 512? The standard for images is usually integers between 0-255 or floats between 0 and 1; but my guess is that astronomy requires a larger bit depth? In any case, you should also make sure the dtype of your numpy array is large enough for the desired bit depth - often images are stored with dtype="uint8" which ranges from 0 to 255.</p>

CC BY-SA 4.0
2019-06-10T16:58:52.727Z
Hello, thanks for your reply. I tried your splitting the r,g,b channels and saving a .fits file for each channel like you described. It works, but the observed intensities are clearly quite different than the .png file. It makes me think I need to do some kind of transformation to the values before storing them to give a good result, but I don't really know what that is. Also, you're right about the 0-255 range, that was my mistake.
user3042966
CC BY-SA 4.0
2019-06-10T17:09:00.260Z
I uploaded a sample png with a .fits for the r,g,b channels here: [link](https://drive.google.com/file/d/19Z3YZDhU-tuQuRy7DyqWIb2QQNuytdfY/view?usp=sharing)
user3042966
CC BY-SA 4.0
2020-01-07T09:30:57.813Z
@user3042966 What are you observing the intensities with? Astronomical software such as ds9 may just be setting the colorscale to log rather than linear values, giving the impression that the intensities are off.
usernumber
CC BY-SA 4.0
2019-06-10T04:00:41.467Z
2019-06-10T04:18:05.363Z
2019-06-10T04:18:05.363Z
Nitesh Menon
<p>I am trying to save a NumPy RGB array (a 3d array with dimension 512x512x3), to a .fits file using <code>astropy.io</code>.
Saving this array to a .png file works fine, but when I'm trying to save it to a .fits file using <code>HDUList.writeto</code>, it produces just garbage colors. 
Also, I have to reshape the image like so:</p>

<pre><code>img = img.reshape(img.shape[2], img.shape[0], img.shape[1])
</code></pre>

<p>or it will try to store just 3 column of the image instead of 512.</p>

<p>Each element in the 3d array contains a value between 0 and 255.</p>

CC BY-SA 4.0
2019-06-10T13:19:03.377Z
What does "it produces just garbage colors" mean? FITS files aren't meant for storing RGB "colors".
Peter Erwin
<python><astropy><image-processing>
Converting an RGB image to fits (astropy)
user3042966
# Converting an RGB image to fits (astropy)

(Asked by: username_0 on 2019-06-09)

I am trying to save a NumPy RGB array (a 3d array with dimension 512x512x3), to a .fits file using astropy.io.
Saving this array to a .png file works fine, but when I'm trying to save it to a .fits file using HDUList.writeto, it produces just garbage colors. 
Also, I have to reshape the image like so:
img = img.reshape(img.shape[2], img.shape[0], img.shape[1])

or it will try to store just 3 column of the image instead of 512.
Each element in the 3d array contains a value between 0 and 255.

**Tags:** python, astropy, image-processing
> **Comments:**
> **username_1**: What does "it produces just garbage colors" mean? FITS files aren't meant for storing RGB "colors".  

---

## 1 answers

### Answer by username_2 on 2019-06-10. Score: 4

I haven't done much astronomical image processing before, but as this question is unanswered I'll give it a shot - hopefully to some avail. If the problem is more specific, a code sample/image sample would probably be useful for further diagnosis, but otherwise this example may help. It discusses the process of writing a 3-channel image to separate FITS images. I would try this first, and check if this output is also garbage - it might just be an issue with how you're using the HDUList.writeto() function. The relevant code is pasted below (with a couple of edits, as the example uses the Pillow Image class at first):
Split the three channels (RGB):
r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]

Write out the channels as separate FITS images
red = fits.PrimaryHDU(data=r)
red.writeto('red.fits')

green = fits.PrimaryHDU(data=g)
green.writeto('green.fits')

blue = fits.PrimaryHDU(data=b)
blue.writeto('blue.fits')

Also out of curiosity - why is your "numpy RGB array" storing values between 0 and 512? The standard for images is usually integers between 0-255 or floats between 0 and 1; but my guess is that astronomy requires a larger bit depth? In any case, you should also make sure the dtype of your numpy array is large enough for the desired bit depth - often images are stored with dtype="uint8" which ranges from 0 to 255.

> **Comments:**
> **username_0**: Hello, thanks for your reply. I tried your splitting the r,g,b channels and saving a .fits file for each channel like you described. It works, but the observed intensities are clearly quite different than the .png file. It makes me think I need to do some kind of transformation to the values before storing them to give a good result, but I don't really know what that is. Also, you're right about the 0-255 range, that was my mistake.  
> **username_0**: I uploaded a sample png with a .fits for the r,g,b channels here: [link](https://drive.google.com/file/d/19Z3YZDhU-tuQuRy7DyqWIb2QQNuytdfY/view?usp=sharing)  
> **username_3**: @user3042966 What are you observing the intensities with? Astronomical software such as ds9 may just be setting the colorscale to log rather than linear values, giving the impression that the intensities are off.  

---

