Skip to content

Instantly share code, notes, and snippets.

@dennisdebel
Created September 3, 2026 09:24
Show Gist options
  • Select an option

  • Save dennisdebel/d67263c737f338e72eaf0705bcbf25ff to your computer and use it in GitHub Desktop.

Select an option

Save dennisdebel/d67263c737f338e72eaf0705bcbf25ff to your computer and use it in GitHub Desktop.
RAWFIX: Fix corrupt Canon Raw Files
# RAWFIX: Fix corrupted Canon RAW files using a working donor image and exiftools
#
# Prerequisits: python3, exiftool
#
# Usage: just copy paste in python3 interactive mode and press enter twice,
# after running the script, exit python and fix the repaired file (offset) using:
# $ exiftool -tagsFromFile /Users/a/Downloads/recover-raw/3B1A5335.CR2 -all:all /Users/a/Downloads/recover-raw/3B1A5336-repaired.CR2
# optionally you can rebuild meta data so the file is more compatible: exiftool -overwrite_original -tagsFromFile /Users/a/Downloads/recover-raw/3B1A5335.CR2 -all:all /Users/apple/Downloads/recover-raw/3B1A5336-repaired.CR2
import os
donor_path = "/Users/a/Downloads/recover-raw/3B1A5335.CR2"
corrupt_path = "/Users/a/Downloads/recover-raw/3B1A5336-corrupt.CR2"
output_path = "/Users/a/Downloads/recover-raw/3B1A5336-repaired.CR2"
# Standard Canon CR2 header size safety range (128 KB)
HEADER_SIZE = 128 * 1024
if not os.path.exists(donor_path) or not os.path.exists(corrupt_path):
print("Error: Input files not found.")
exit(1)
with open(donor_path, "rb") as f_donor:
donor_header = f_donor.read(HEADER_SIZE)
with open(corrupt_path, "rb") as f_corrupt:
# Skip damaged header area of corrupted file and retain sensor data stream
f_corrupt.seek(HEADER_SIZE)
corrupt_body = f_corrupt.read()
with open(output_path, "wb") as f_out:
f_out.write(donor_header)
f_out.write(corrupt_body)
print(f"Repaired file written to: {output_path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment