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.
33 lines
1.1 KiB
Bash
Executable File
33 lines
1.1 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# images_resize Resizing large JPGs in current directory
|
|
# Based on: https://shkspr.mobi/blog/2016/12/converting-rar-to-zip-in-linux/
|
|
#
|
|
# Usage: xxx.sh
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
source "$SCRIPT_DIR/config.sh"
|
|
|
|
echo "Resizing large JPGs in $(pwd)"
|
|
|
|
find . -regextype posix-egrep -regex ".*\.(jpg|JPG|JPEG|jpeg)$" -print0 | while IFS= read -r -d '' f
|
|
do
|
|
# Skip files already processed with these parameters
|
|
COMMENT=$(~/Applications/magick identify -format "%c" "$f" 2>/dev/null)
|
|
if [[ "$COMMENT" == "$MAGICK_COMMENT" ]]; then
|
|
echo "Skipping $f: already processed"
|
|
continue
|
|
fi
|
|
|
|
# Get file size before resizing
|
|
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% -set comment "$MAGICK_COMMENT" "$f"
|
|
|
|
# Get file size after resizing
|
|
SIZE_AFTER=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null)
|
|
|
|
# Display before/after sizes
|
|
echo "Resized $f: $(numfmt --to=iec-i --suffix=B $SIZE_BEFORE) -> $(numfmt --to=iec-i --suffix=B $SIZE_AFTER)"
|
|
done
|