0

I am trying to convert a raw cr2 image into .fits using imageio and PIL. But, I am unable to convert the image into .fits format

I am currently converting .cr2 into jpg as I am unable to convert .cr2 into .fits. (if possible = best). After conversion into jpg, I am opening the file and splitting the r,g,b into 3 different arrays and pass each array to a separate .fits file but, whenever I try to pass the entire data of r,g,b combined to array it never works.

import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt
from PIL import Image
im = imageio.imread('E:\FYP\cr.cr2', format="RAW-FI")
imageio.imwrite(r'E:\FYP\1.jpg', im)
image = Image.open(r'E:\FYP\1.jpg')
xsize, ysize = image.size
print("Image size: {} x {}".format(xsize, ysize))
plt.imshow(image)
r, g, b = image.split()
r_data = np.array(r.getdata()) # data is now an array of length ysize*xsize
g_data = np.array(g.getdata())
b_data = np.array(b.getdata())
print(r_data.shape)
r_data = r_data.reshape(ysize, xsize)
g_data = g_data.reshape(ysize, xsize)
b_data = b_data.reshape(ysize, xsize)
red = fits.PrimaryHDU(data=r_data)
red.header['LATOBS'] = "32:11:56" # add spurious header info
red.header['LONGOBS'] = "110:56"
red.writeto(r'E:\FYP\red.fits')

It gives me single-colored .fits image while i want is an image with all R,G,B colors as .fits image.

lw1.at
  • 1,528
  • 11
  • 25
  • 1
    You are only saving the red matrix into the FITS file: `red = fits.PrimaryHDU(data=r_data)` – lw1.at Sep 09 '19 at 18:02
  • FITS files store arbitrary arrays: Images are typically intensity values of some kind (depending on the instrument) in a single color channel. Some users might opt to create a *multi-extension* FITS file containing one 2-D array per channel, usinig the `FILTER` keyword in each header to distinguish them. Alternatively you can output each to a single file. Or a single 3-D array in a single extension file. It depends on how the file is to be processed later. – Iguananaut Sep 10 '19 at 07:52
  • As far as I can tell from your [other question](https://stackoverflow.com/questions/57846507/how-to-create-fits-file-from-numpy-array?noredirect=1#comment102144224_57846507) you *are* able to convert directly to a FITS file, but perhaps you are not formatting the resulting FITS file in a way understood by CCDStack. Here is a [python script](https://github.com/eaydin/cr2fits) that converts raw CR2 images to FITS. It requires compiling a little external C program as well. In terms of software design it could use some work, but it does seem to do the job well. – Iguananaut Sep 10 '19 at 08:01
  • I have created my own script but, your comments were helpful. Thanks for the suggestions. – Burhan Khan Sep 11 '19 at 13:36

0 Answers0