Skip images already processed by checking embedded JPEG comment

Replace pixel dimension check with a JPEG comment check. Processed
files are marked with 'magick:resize=8mp,quality=85,sampling=4:2:0'
via -set comment after -strip, so future runs skip them entirely.
This commit is contained in:
2026-05-02 19:32:18 -04:00
parent fff8e346ad
commit 2ed392d466
+6 -5
View File
@@ -8,20 +8,21 @@
echo "Resizing large JPGs in $(pwd)" echo "Resizing large JPGs in $(pwd)"
MAX_PIXELS=8000000 MAX_PIXELS=8000000
MAGICK_COMMENT="magick:resize=8mp,quality=85,sampling=4:2:0"
find . -regextype posix-egrep -regex ".*\.(jpg|JPG|JPEG|jpeg)$" -print0 | while IFS= read -r -d '' f find . -regextype posix-egrep -regex ".*\.(jpg|JPG|JPEG|jpeg)$" -print0 | while IFS= read -r -d '' f
do do
# Check pixel count before processing # Skip files already processed with these parameters
read -r IMG_W IMG_H < <(~/Applications/magick identify -format "%w %h" "$f" 2>/dev/null) COMMENT=$(~/Applications/magick identify -format "%c" "$f" 2>/dev/null)
if [[ -n "$IMG_W" && -n "$IMG_H" && $((IMG_W * IMG_H)) -le "$MAX_PIXELS" ]]; then if [[ "$COMMENT" == "$MAGICK_COMMENT" ]]; then
echo "Skipping $f: ${IMG_W}x${IMG_H} already within limit" echo "Skipping $f: already processed"
continue continue
fi fi
# Get file size before resizing # Get file size before resizing
SIZE_BEFORE=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null) SIZE_BEFORE=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null)
~/Applications/magick "$f" -resize "${MAX_PIXELS}@@\>" -sampling-factor 4:2:0 -strip -quality 85% "$f" ~/Applications/magick "$f" -resize "${MAX_PIXELS}@@\>" -sampling-factor 4:2:0 -strip -quality 85% -set comment "$MAGICK_COMMENT" "$f"
# Get file size after resizing # Get file size after resizing
SIZE_AFTER=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null) SIZE_AFTER=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null)