Canon CR2 File Auto-stretch and Exposure Clipping Analysis Script
Batch CR2 Histogram and Clipping Analysis Script
Download here (beta - feedback welcome)
Who it’s for:
This script is for astrophotographers using Canon CR2 files who are testing exposures on a new target. If you take several test frames at different exposure lengths, such as 30s, 60s, 120s, or 240s, this tool lets you quickly compare them to find the optimal exposure for your target and environment.
Why it’s useful:
Choosing the right exposure is critical. Too short and faint details are lost. Too long and bright stars or background sky can clip. While other tools can show image statistics frame by frame, this script loads multiple test frames at once, producing a visual comparison and numeric analysis in one view. You can immediately see which exposure maximizes signal while avoiding clipping.
How it works:
- Reads your CR2 files and calculates clipped pixels in the 14-bit RAW sensor data.
- Calculates practical clipping after auto-stretch for visualization in 8-bit.
- Generates a combined histogram showing all selected frames in one plot with different colors.
Adds 1/3 and 2/3 reference lines to help interpret exposure relative to the sensor’s dynamic range.
- Saves auto-stretched PNG images for each frame and a combined histogram PNG.
How to use it:
- Capture a few test frames of your target at different exposures.
- Place the CR2 files in a folder.
- Run the script from the command line:
python autostretch_cr2.py <folder_or_file> - The script will create a
stretchedfolder, save auto-stretched images, and generate a combined histogram. - Check the console output for RAW and 8-bit clipped pixel counts and inspect the histogram to choose the best exposure.
Why this script is better than standard tools:
- Compare multiple exposures at once instead of checking frames individually.
- Counts clipped pixels at the RAW sensor level for accuracy.
- Provides 8-bit histograms for quick visual comparison.
- 1/3 and 2/3 reference lines make it easier to judge exposure relative to sensor range.
Requirements:
- Python 3
- Packages: rawpy, numpy, matplotlib, opencv-python
- Canon CR2 RAW files (tested with 600D/T3i)
Place the py file in a folder with your test exposures. Run the command line from here.

It will created a 'stretched' sub-folder. The plot is the most useful result.

The plot shows for all test exposures a stretched histogram similar to what you would see on the DSLR screen. As a rule of thumb we aim for the peak to be at approximately 1/3 the histogram. More accurately, you should look to the legend at the number of pixels at the maximum bit depth value. This is the number written after "highs". Typically a few hundred is considered acceptable as in this 60s exposure example.