Convert DSLR RAW Images to Astronomical FITS Images
Branch: master
Clone or download
eaydin fixed typo
resolve issue #5
thanks to @michael-petersen for pointing it out!
Latest commit 8bbce82 Jul 26, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
dcraw added dcraw source code May 15, 2017
.gitattributes added git attributes to ignore C code May 21, 2017
.gitignore add DOI May 21, 2017 fixed typo Jul 26, 2017

cr2fits version 2.1.0


A script to convert RAW images (Canon, Nikon etc.) to FITS images. It extracts only one color channel of the RAW image, and writes the necessary EXIF information to the FITS header.


  • Python 2.7 or Python >= 3.2
  • PyFITS or astropy: Older versions cr2fits (1.0.3 and earlier) depended on PyFITS. From 2.0.0 and on it added support for astropy. It uses which is basically the same as PyFITS for now. Favors PyFITS over astropy if you have both, since it is faster to load.
  • dcraw: Try getting the latest version of this. Usually the ones in repositories is not the latest so it's possible that it'll convert your RAW images differently, corrupted. Get the latest version here : Also the one distributed with this package should work.

Compiling dcraw:

Either use gcc -o dcraw -O4 dcraw.c -lm -ljasper -ljpeg -llcms2 or gcc -o dcraw -O4 dcraw.c -lm -DNODEPS to compile it.
You can then copy dcraw to your path (ex. /usr/local/bin). In order to copy to your path, you'll probably need root privileges.

Usage <cr2-filename> <color-index>

  • cr2-filename: This will obviously be the name of the file to convert.
  • color-index: Can take one of four values, either 0, 1, 2, 3 which represent Red, Green, Blue and Unscaled Raw respectively.

If you want to use it as a module:

from cr2fits import cr2fits

# Assuming there's a test.cr2 in the current directory
# And we want the Blue Channel
a = cr2fits("test.cr2", 2)

# Read into the raw image

# Now you have a.pbm_bytes which is the output of dcraw
# Get the EXIF data

# Now you have values such as a.shutter,, a.iso ...
# Convert dcraw output to Numpy Array using netpbm
im_ppm = a.read_pbm(a.pbm_bytes)

# Since we want only one channel (ex. Blue) use get_color
im_blue = a.get_color(im_ppm, a.colorInput)

# Create FITS file from Blue Image and EXIF data
fits_image = a.create_fits(im_blue)

# If you want to write output, _generate_destination gets filename
dest = a._generate_destination(a.filename, a.colorInput)

# Write FITS file to generated destination (or elsewhere)
a.write_fits(fits_image, dest)

Also, the method convert does all this for you.

from cr2fits import cr2fits
a = cr2fits("test.cr2", 2)


The script will output the FITS file, added the color channel to the name of the output.

Change log


  • Added support for unscaled raw output.
  • Fixed output name detection fails with dot's (.) in the name.
  • Detects if output filename exists and gives alternative name


  • Added support for astropy
  • Added support for usage as a module
  • Use Numpy slicing instead of looping
  • Direct dcraw output to BytesIO object, avoiding the I/O for the unused PPM file
  • Updated license


  • Add support for configparser
  • Add support for 3D FITS files: an idea from the fork by @mireianievas
  • Add support for multiple file inputs