[PYTHON] Demosaic Bayer FITS files and convert them to color TIFF

There is no handy tool for converting FITS files on macOS and Linux. Learn how to use Python to demomosate Bayer FITS files and convert them to TIFF format that can be edited in Photoshop.

Shooting environment

--Camera - ZWO ASI 294MC --Control software - KStars + Ekos

The Bayer pattern for the ZWO ASI 294MC is RGGB.

Environment

Use Astropy to read the FITS file and OpenCV for image processing. The package version at the time of writing is as follows.

processing

Let's process the image of the moon (moon.fits) taken with a telescope.

1. Open FITS file

from astropy.io import fits

hdul = fits.open('moon.fits')
src = hdul[0].data

At this stage, the Bayer array remains, so it is a monochrome mosaic image.

test3-bayer_tif.png

2. Demosaic / Save

Restore a color image by specifying a Bayer transform with the cvtColor function.

import cv2
dst = cv2.cvtColor(src, cv2.COLOR_BayerBG2BGR)
cv2.imwrite('moon.tif', dst)

The following TIFF image was output. You can adjust the brightness and color balance with any image editing software.

moon.jpg

3. Editing example in Photoshop

Level correction, color balance adjustment, and sharpening filter applied.

moon-edited.jpg

Recommended Posts

Demosaic Bayer FITS files and convert them to color TIFF
Script to convert between Xcode language files and tab-delimited text
I made a network to convert black and white images to color images (pix2pix)
[Linux] How to subdivide files and folders
Convert MetaImageIO (* .mha, * .mhd) to tiff stack
Convert PDF files to PNG files with GIMP
Convert HEIC files to PNG files with Python
Create an API to convert PDF files to TIF images with FastAPI and Docker