81 lines
2.1 KiB
Bash
81 lines
2.1 KiB
Bash
#!/bin/bash
|
|||
|
|
|
||
|
|
###############################################################################
|
||
|
|
# Script Name: crop_images.sh
|
||
|
|
#
|
||
|
|
# Description:
|
||
|
|
# Safely processes JPEG images in the current directory. If an image has a
|
||
|
|
# width of 3546 pixels, the script:
|
||
|
|
# 1. Creates a cropped copy (2448x3264 from the top-left corner).
|
||
|
|
# 2. Saves the cropped image as *_c.jpg.
|
||
|
|
# 3. Renames the original image as *_u.jpg.
|
||
|
|
#
|
||
|
|
# Features:
|
||
|
|
# - Processes only .jpg files.
|
||
|
|
# - Handles filenames containing spaces.
|
||
|
|
# - Verifies that ImageMagick is installed.
|
||
|
|
# - Skips files that have already been processed.
|
||
|
|
# - Uses ImageMagick's format output instead of parsing command output.
|
||
|
|
#
|
||
|
|
# Requirements:
|
||
|
|
# - Bash
|
||
|
|
# - ImageMagick (identify, convert)
|
||
|
|
#
|
||
|
|
# Usage:
|
||
|
|
# chmod +x crop_images.sh
|
||
|
|
# ./crop_images.sh
|
||
|
|
#
|
||
|
|
###############################################################################
|
||
|
|
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
# Verify required commands exist
|
||
|
|
for cmd in identify convert; do
|
||
|
|
command -v "$cmd" >/dev/null 2>&1 || {
|
||
|
|
echo "Error: '$cmd' command not found. Install ImageMagick."
|
||
|
|
exit 1
|
||
|
|
}
|
||
|
|
done
|
||
|
|
|
||
|
|
# Process JPEG files only
|
||
|
|
for img in *.jpg *.JPG; do
|
||
|
|
|
||
|
|
# Skip if no matching files exist
|
||
|
|
[[ -f "$img" ]] || continue
|
||
|
|
|
||
|
|
# Skip files already processed
|
||
|
|
[[ "$img" =~ _c\.jpg$|_u\.jpg$ ]] && continue
|
||
|
|
|
||
|
|
# Get image dimensions (e.g. 3546x4728)
|
||
|
|
size=$(identify -format "%wx%h" "$img" 2>/dev/null) || {
|
||
|
|
echo "Skipping invalid image: $img"
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
# Process only images whose width is exactly 3546 pixels
|
||
|
|
if [[ "$size" == 3546x* ]]; then
|
||
|
|
|
||
|
|
echo "Processing: $img ($size)"
|
||
|
|
|
||
|
|
cropped="${img%.jpg}_c.jpg"
|
||
|
|
original="${img%.jpg}_u.jpg"
|
||
|
|
|
||
|
|
# Handle uppercase extension
|
||
|
|
if [[ "$img" == *.JPG ]]; then
|
||
|
|
cropped="${img%.JPG}_c.jpg"
|
||
|
|
original="${img%.JPG}_u.jpg"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Create cropped image
|
||
|
|
convert "$img" -crop 2448x3264+0+0 "$cropped"
|
||
|
|
|
||
|
|
# Rename original image
|
||
|
|
mv "$img" "$original"
|
||
|
|
|
||
|
|
echo " Created: $cropped"
|
||
|
|
echo " Renamed: $original"
|
||
|
|
fi
|
||
|
|
|
||
|
|
done
|
||
|
|
|
||
|
|
echo "Done."
|