27 de mayo de 2024

Convert RAW photos to JPG with macOS and delete original RAWs after checking the conversion

The command recursively searches for .cr2 and .dng files in the current directory and all its subdirectories, converts them to high-quality JPEGs with a resolution of 300 dpi and a color depth of at least 8 bits per channel, and then deletes the original files if the conversion is successful.

find . -type f \( -iname "*.cr2" -o -iname "*.dng" \) | while read -r FILE; do EXT="${FILE##*.}"; BASE="${FILE%.*}"; NEWFILE="${BASE}-${EXT}.jpg"; sips -s format jpeg -s formatOptions best -s dpiWidth 300 -s dpiHeight 300 "$FILE" --out "$NEWFILE"; if [ -f "$NEWFILE" ]; then rm "$FILE"; else echo "Error converting $FILE"; fi; done


  1. find . -type f \( -iname "*.cr2" -o -iname "*.dng" \):

    • Recursively searches for files with .cr2 or .dng extensions starting from the current directory (.).
  2. | while read -r FILE; do ... done:

    • For each file found, the command within this loop is executed.
  3. EXT="${FILE##*.}":

    • Extracts the file extension (either cr2 or dng).
  4. BASE="${FILE%.*}":

    • Removes the file extension to get the base file name.
  5. NEWFILE="${BASE}-${EXT}.jpg":

    • Constructs the new JPEG file name in the format original-name-cr2.jpg or original-name-dng.jpg.
  6. sips -s format jpeg -s formatOptions best -s dpiWidth 300 -s dpiHeight 300 "$FILE" --out "$NEWFILE":

    • Converts the file to JPEG format using sips:
      • -s format jpeg: Specifies the output format as JPEG.
      • -s formatOptions best: Sets the JPEG quality to the best.
      • -s dpiWidth 300 -s dpiHeight 300: Sets the resolution to 300 dpi.
      • "$FILE": Specifies the input file.
      • --out "$NEWFILE": Specifies the output file with the new name.
  7. if [ -f "$NEWFILE" ]; then rm "$FILE"; else echo "Error converting $FILE"; fi:

    • Checks if the new JPEG file was successfully created:
      • If the new file exists, deletes the original file.
      • If the new file does not exist, outputs an error message indicating the conversion failed.