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.
This commit is contained in:
2026-07-03 19:32:04 -04:00
parent 2ed392d466
commit 6901c66bcf
3 changed files with 35 additions and 6 deletions
+5
View File
@@ -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"
+3 -3
View File
@@ -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
+27 -3
View File
@@ -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"