From JPEG to FITS: A Beginner’s Guide to jpeg2fits
What this guide covers
- Purpose: Convert standard JPEG images into FITS (Flexible Image Transport System) files commonly used in astronomy and scientific imaging.
- Audience: Beginners with basic command-line familiarity who want a quick, reliable conversion tool.
- Outcome: Produce FITS files preserving pixel values, add basic metadata, and prepare images for scientific analysis.
Why convert JPEG → FITS
- FITS is standard for astronomical data and supports multidimensional arrays and extensive metadata.
- Metadata support: Store observation details (date, instrument, exposure) in header keywords.
- Analysis-ready: FITS works with astronomy tools (ds9, Astropy, IRAF) while JPEG does not.
Typical workflow (assumes jpeg2fits command-line tool)
-
Install jpeg2fits
- Common methods: pip, prebuilt binaries, or package manager (assume pip if Python-based).
- Example:
pip install jpeg2fits(replace with actual install command for your distro).
-
Basic conversion
- Command:
jpeg2fits input.jpg output.fits - Result: single-extension FITS file with image data in primary HDU.
- Command:
-
Preserve or set metadata
- Add header keywords:
jpeg2fits input.jpg output.fits –add KEY=VALUE - Common keys: DATE-OBS, INSTRUME, TELESCOP, EXPTIME, COMMENT.
- Add header keywords:
-
Batch conversion
- Example (bash):
for f in.jpg; do jpeg2fits “\(f" "\){f%.jpg}.fits”; done
- Example (bash):
-
Scaling and datatype
- Options to control scaling (normalize, stretch) and output datatype (e.g., 16-bit int, float).
- Example flags:
–scale noneor–dtype float32(tool-specific).
-
Verify and view
- Inspect header:
fitsinfo output.fitsorpython -c “from astropy.io import fits; print(fits.open(‘output.fits’)[0].header)” - View image:
ds9 output.fitsor matplotlib in Python.
- Inspect header:
Example using Python + Astropy (if jpeg2fits unavailable)
- Read JPEG, convert to FITS, add header, save:
python
from PIL import Image import numpy as np from astropy.io import fits img = Image.open(‘input.jpg’).convert(‘L’) # grayscale; use ‘RGB’ to keep channels data = np.array(img) hdu = fits.PrimaryHDU(data.astype(‘uint16’)) hdu.header[‘COMMENT’] = ‘Converted from JPEG on 2026-02-05’ hdu.writeto(‘output.fits’, overwrite=True)
Tips & caveats
- JPEG is lossy: original pixel values are approximated; not ideal for precision photometry.
- For scientific use, prefer RAW, TIFF, or native instrument formats when available.
- Keep a record of any scaling applied during conversion in the FITS header.
Further reading / next steps
- Learn FITS header conventions (FITS Standard).
- Use Astropy for advanced header editing and multi-extension FITS.
- Integrate conversion into preprocessing pipelines for stacking, calibration, or photometry.
(Date: 2026-02-05)
Leave a Reply