From 6901c66bcfc911d5f25a85bce1efa1f6033c6aad Mon Sep 17 00:00:00 2001 From: jim kolpack Date: Fri, 3 Jul 2026 19:32:04 -0400 Subject: [PATCH] Skip already-processed ZIPs by sampling first JPEG comment Extracts one JPEG from each ZIP to check for the processing comment before doing a full extract/resize/rezip cycle. Shared constants moved to config.sh as single source of truth. Trap and cleanup extended to cover the sample temp file. --- config.sh | 5 +++++ images_resize.sh | 6 +++--- zip_images_resize.sh | 30 +++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 config.sh diff --git a/config.sh b/config.sh new file mode 100644 index 0000000..e3124ed --- /dev/null +++ b/config.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# Shared configuration for image processing scripts + +MAX_PIXELS=8000000 +MAGICK_COMMENT="magick:resize=8mp,quality=85,sampling=4:2:0" diff --git a/images_resize.sh b/images_resize.sh index 7f91fbf..dc75643 100755 --- a/images_resize.sh +++ b/images_resize.sh @@ -5,10 +5,10 @@ # # Usage: xxx.sh -echo "Resizing large JPGs in $(pwd)" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$SCRIPT_DIR/config.sh" -MAX_PIXELS=8000000 -MAGICK_COMMENT="magick:resize=8mp,quality=85,sampling=4:2:0" +echo "Resizing large JPGs in $(pwd)" find . -regextype posix-egrep -regex ".*\.(jpg|JPG|JPEG|jpeg)$" -print0 | while IFS= read -r -d '' f do diff --git a/zip_images_resize.sh b/zip_images_resize.sh index 5b5d755..9447991 100755 --- a/zip_images_resize.sh +++ b/zip_images_resize.sh @@ -11,6 +11,7 @@ if [ $# -eq 0 ]; then fi SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$SCRIPT_DIR/config.sh" echo "Resizing large JPGs in ZIPs" @@ -19,6 +20,9 @@ WORKDIR="/dev/shm/" # Cleanup function cleanup_temp() { + if [ -n "$SAMPLE" ] && [ -f "$SAMPLE" ]; then + rm -f "$SAMPLE" + fi if [ -n "$TEMPDIR" ] && [ -d "$TEMPDIR" ]; then rm -r "$TEMPDIR" fi @@ -35,6 +39,29 @@ for INFILE in "$@"; do INFILE="$(realpath "$INFILE")" echo "Processing file $CURRENT_FILE of $TOTAL_FILES: $INFILE" + SAMPLE="" + TEMPDIR="" + trap cleanup_temp EXIT INT TERM + + # Check if already processed by sampling the first JPEG in the archive + FIRST_JPEG=$(unzip -l "$INFILE" 2>/dev/null | grep -iE '\.(jpg|jpeg)$' | head -1 | awk '{print $NF}') + if [ -n "$FIRST_JPEG" ]; then + SAMPLE=$(mktemp -p "$WORKDIR" --suffix=.jpg) + if unzip -p "$INFILE" "$FIRST_JPEG" > "$SAMPLE" 2>/dev/null; then + COMMENT=$(~/Applications/magick identify -format "%c" "$SAMPLE" 2>/dev/null) + rm -f "$SAMPLE" + SAMPLE="" + if [[ "$COMMENT" == "$MAGICK_COMMENT" ]]; then + echo "Skipping $INFILE: already processed" + trap - EXIT INT TERM + continue + fi + else + rm -f "$SAMPLE" + SAMPLE="" + fi + fi + OUTFILE="$(realpath "${INFILE%.zip}-compress.zip")" # Remove output file if it already exists @@ -45,9 +72,6 @@ for INFILE in "$@"; do # Set name for the temp dir. This directory will be created under WORKDIR TEMPDIR=$(mktemp -p "$WORKDIR" -d) - # Set up cleanup trap - trap cleanup_temp EXIT INT TERM - # Create a temporary folder for unzipped files echo "Extracting $INFILE"