This script crops FITS images while preserving important metadata—including updating the World Coordinate System (WCS) information in the FITS header. It is useful for batch processing astronomical images when you need to extract a region of interest.
- Batch Processing: Processes all FITS files in a specified source folder.
- Accurate Cropping: Uses user-defined starting coordinates and dimensions (width and height in pixels) to crop images.
- Metadata Preservation: Updates the FITS header with the new image dimensions and preserves key metadata (e.g.,
EXPTIME
,DATE-OBS
,FILTER
,TELESCOP
,INSTRUME
). - WCS Adjustment: Automatically updates the WCS reference pixel values (
CRPIX1
andCRPIX2
) if present. - Multi-HDU Support: Finds the first HDU containing valid 2D image data.
- Command-Line & GUI Options: Accepts crop parameters via command-line arguments, or falls back to GUI dialogs (using Tkinter) and console input.
- Logging: Uses Python’s
logging
module to output information and error messages.
Install the required Python packages (if not already installed) via pip:
pip install astropy numpy
The script is mainly designed for a user-friendly GUI workflow:
- Folder Selection:
A Tkinter dialog lets you choose the source folder (with FITS files) and the destination folder (for saving cropped images). - Crop Parameters:
You are prompted via the console to enter:x_start
andy_start
: The 0-indexed pixel coordinates of the upper-left corner of the crop.x_size
andy_size
: The width and height (in pixels) of the crop.
- Alternative Usage (Command-Line Mode):
For more advanced or automated workflows, you can provide parameters via command-line arguments. The following commands are available: Basic Command-Line Example:python resize.py --source_folder /path/to/source \ --destination_folder /path/to/destination \ --x_start 100 --y_start 100 \ --x_size 500 --y_size 500
- Source Folder: The directory containing your original FITS files.
- Destination Folder: The directory where cropped FITS files will be saved.
- x_start & y_start: The 0-indexed pixel coordinates (in your Python array) marking the upper-left corner of the crop.
- x_size & y_size: The width and height (in pixels) of the crop. For example, an
x_size
of 300 means the cropped image will include 300 pixels in the horizontal direction. - WCS Update: If the FITS header includes WCS keywords (like
CRPIX1
andCRPIX2
), the script adjusts these values to reflect the crop. Note that while FITS headers use 1-indexed coordinates, the cropping uses 0-indexed pixel positions in the Python array.
The script logs progress and errors to the console using Python’s logging
module. This helps you track which files were processed, skipped, or encountered errors.