Files
image_processing/zip_images_resize.sh
poprhythm fff8e346ad Make scripts generic and fix several bugs
- Add optional directory argument to images_resize_recursive.sh,
  zip_images_resize_recursive.sh, and new rar2zip_recursive.sh
- Remove hardcoded-path wrappers rar2zip_images.sh and rar2zip_incomplete.sh
- Fix zip_images_resize.sh: move SCRIPT_DIR to top, change exit 1 to
  continue on bad output file so remaining files still process
- Use MAX_PIXELS variable in images_resize.sh resize argument
- Fix zip_images_recursive.sh: replace case-sensitive 7z globs with
  find -iregex piped via xargs
2026-05-01 21:58:31 -04:00

108 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
#
# zip_images_resize Resizing large JPGs in ZIPs
# Based on: https://shkspr.mobi/blog/2016/12/converting-rar-to-zip-in-linux/
#
# Usage: xxx.sh file [file ...]
if [ $# -eq 0 ]; then
echo "Usage: $0 file.zip [file2.zip ...]"
exit 1
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "Resizing large JPGs in ZIPs"
# Use RAM disk for temporary files.
WORKDIR="/dev/shm/"
# Cleanup function
cleanup_temp() {
if [ -n "$TEMPDIR" ] && [ -d "$TEMPDIR" ]; then
rm -r "$TEMPDIR"
fi
}
# Count total files
TOTAL_FILES=$#
CURRENT_FILE=0
for INFILE in "$@"; do
CURRENT_FILE=$((CURRENT_FILE + 1))
# Convert to absolute path
INFILE="$(realpath "$INFILE")"
echo "Processing file $CURRENT_FILE of $TOTAL_FILES: $INFILE"
OUTFILE="$(realpath "${INFILE%.zip}-compress.zip")"
# Remove output file if it already exists
if [ -e "${OUTFILE}" ]; then
rm "${OUTFILE}"
fi
# 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"
# extract files from zip
if ! unzip "$INFILE" -d "$TEMPDIR"; then
echo "Error: Failed to extract $INFILE"
cleanup_temp
continue
fi
# resize
#convert '${TEMPDIR}/*.jpg' -set filename:fn '%[basename]' -resize 2800000@@\> -sampling-factor 4:2:0 -strip -quality 85% -colorspace RGB '%[filename:fn].jpg'
if ! pushd "$TEMPDIR" > /dev/null; then
echo "Error: Failed to change to temp directory"
cleanup_temp
continue
fi
"$SCRIPT_DIR/images_resize.sh"
popd > /dev/null
# Zip the files with no compression
pushd "$TEMPDIR" > /dev/null
if ! 7z a -tzip -mx=0 "$OUTFILE" ./*; then
echo "Error: Failed to create ZIP"
popd > /dev/null
cleanup_temp
continue
fi
popd > /dev/null
# Alternative. MUCH SLOWER, but better compression
# 7z a -mm=Deflate -mfb=258 -mpass=15 -r "$NEWNAME" *
# Preserve file modification time
touch -r "$INFILE" "$OUTFILE"
# Check that OUTFILE exists and is not empty before deleting INFILE
if [ ! -s "$OUTFILE" ]; then
echo "Error: Output file is empty or does not exist. Keeping original file."
cleanup_temp
continue
fi
rm "$INFILE"
mv "$OUTFILE" "$INFILE"
# Delete the temporary directory
cleanup_temp
trap - EXIT INT TERM
# OPTIONAL. Safe-remove the old file
#gio trash "$OLDFILE"
done
echo "Conversion Done"