feat(images): consolidate panorama detection

Combine panorama detection utilities into a single script.

- Merge aspect-ratio based panorama detection with dimension-based
checks
- Detect images with aspect ratio < 0.5 or > 1.8
- Detect images where width exceeds three times the height
- Ignore images already tagged as Panorama
- Scan directories recursively for JPG, JPEG, and PNG files
- Add timestamped logging of detected panorama candidates
- Optionally copy detected panoramas to a review directory
- Improve filename handling using find -print0
- Use identify -format for reliable dimension extraction
- Add dependency validation and robust error handling
- Reduce repeated ImageMagick parsing and simplify maintenance
This commit is contained in:
Lucas Vivohost committed 2026-09-14 12:43:38 +02:00
1 parent c1ebc80235
commit 16c69ac7df
3 files changed
+105 -47

No files matched your search

+105
View File
@@ -0,0 +1,105 @@
#!/bin/bash
###############################################################################
# Script Name: find_panoramas.sh
#
# Description:
# Recursively scans a directory for JPG and PNG images and identifies
# potential panorama images using multiple detection methods:
#
# 1. Aspect ratio < 0.5
# 2. Aspect ratio > 1.8
# 3. Width > 3 × Height
#
# Images already tagged as "Panorama" are ignored.
#
# Matching images are:
# - Logged to a timestamped logfile
# - Optionally copied to a review directory
#
# Requirements:
# - Bash
# - ImageMagick (identify)
# - bc
#
# Usage:
# ./find_panoramas.sh
#
###############################################################################
set -euo pipefail
IMG_DIR="/home/lucas/Pictures"
COPY_DIR="/home/lucas/Downloads"
PANORAMA_AR_LT="0.5"
PANORAMA_AR_HT="1.8"
DATE=$(date +"%Y%m%d_%H%M%S")
LOGFILE="${IMG_DIR}/panorama_${DATE}.log"
touch "$LOGFILE"
write_log() {
echo "$@"
echo "$@" >> "$LOGFILE"
}
# Verify dependencies
for cmd in identify bc; do
command -v "$cmd" >/dev/null 2>&1 || {
echo "Error: '$cmd' not found"
exit 1
}
done
find "$IMG_DIR" \
\( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) \
-print0 |
while IFS= read -r -d '' img; do
# Get dimensions
dimensions=$(identify -format "%wx%h" "$img" 2>/dev/null) || {
write_log "Skipping invalid image: $img"
continue
}
width=${dimensions%x*}
height=${dimensions#*x}
# Avoid divide-by-zero
[[ "$height" -eq 0 ]] && continue
# Calculate aspect ratio
aspect_ratio=$(echo "scale=4; $height / $width" | bc -l)
# Skip images already marked Panorama
if identify -verbose "$img" 2>/dev/null | grep -q "Panorama"; then
continue
fi
panorama=false
# Aspect-ratio checks
if (( $(echo "$aspect_ratio < $PANORAMA_AR_LT" | bc -l) )); then
panorama=true
elif (( $(echo "$aspect_ratio > $PANORAMA_AR_HT" | bc -l) )); then
panorama=true
fi
# Width > 3 × Height check
if (( width > height * 3 )); then
panorama=true
fi
if [[ "$panorama" == true ]]; then
write_log "PANORAMA | ${dimensions} | AR=${aspect_ratio} | ${img}"
# Copy for manual review
cp -n "$img" "$COPY_DIR/" 2>/dev/null || true
fi
done
write_log "Scan complete."
-16
View File
@@ -1,16 +0,0 @@
#!/bin/bash
IFS=$'\n'; set -f
ARR=($(find . \( -name "*.png" -or -name "*.jp*g" \) | grep -v xml))
unset IFS; set +f
for ((i=0;i<${#ARR[@]};i++)); do
NUM1=$(identify "${ARR[i]}" | rev | cut -f7 -d' ' | rev | cut -f1 -d'x')
NUM2=$(identify "${ARR[i]}" | rev | cut -f7 -d' ' | rev | cut -f2 -d'x')
echo "${ARR[i]}"
if [[ "$NUM1" != "" && $NUM2 != "" ]]; then
if [[ $NUM1 -gt $(( $NUM2 * 3 )) ]]; then
identify "${ARR[i]}" >> panoramaImages.txt
cp "${ARR[i]}" /home/lucas/Downloads/
fi
fi
done
-31
View File
@@ -1,31 +0,0 @@
#!/bin/bash
DATE="$(date +'%m%d%H%M%S')"
log="logfile_${DATE}.log"
IMG_DIR="/home/lucas/Pictures/"
PANORAMA_AR_LT="0.5"
PANORAMA_AR_HT="1.8"
> "${IMG_DIR}${log}"
getAspectRatio() {
if [ "$1" == "" ]; then return "0"; fi
identify "$1" | perl -ne '/(.+?)\s+[A-Z]{3,4}\s+(\d+)x(\d+)/; print $3/$2, "\n"'
if [ $? -ne 0 ]; then return "0"; fi
}
write_log() {
echo "$@"
echo "$@" >> "${IMG_DIR}${log}"
}
IFS=$'\n'; set -f
IMG_ARR=($(find ${IMG_DIR} -name '*.jp*g' -or -name '*.JP*G' -name '*.png'))
for IMG in ${IMG_ARR[@]}; do
imageAR=$(getAspectRatio ${IMG})
if [ "${imageAR}" != "" ]; then
if [ $(echo ${imageAR}'<'${PANORAMA_AR_LT} | bc -l) -eq 1 ] || [ $(echo ${imageAR}'>'${PANORAMA_AR_HT} | bc -l) -eq 1 ]; then
if [ $(identify -verbose ${IMG} | grep -o "Panorama" | wc -l) -eq 0 ]; then
write_log ${IMG} ${imageAR}
fi
fi
fi
done