Files
Lucas Vivohost 804e17dbe6 feat(images): add wallpaper candidate collection utility
feat(images): automate wallpaper preparation and filtering

- Recursively scan image directories for JPG, JPEG, and PNG files
- Normalize filenames by replacing spaces with underscores
- Convert JPG/JPEG images to PNG format
- Calculate image aspect ratios automatically
- Filter images using configurable wallpaper criteria
- Validate minimum image width requirements
- Optionally copy matching wallpapers to a collection folder
- Provide progress reporting during processing
- Add script documentation, requirements, and usage examples
2026-09-14 12:43:38 +02:00

141 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
###############################################################################
# Script Name: collect-wallpapers.sh
#
# Description:
# Scans a directory tree for image files and prepares them for wallpaper use.
#
# The script:
# 1. Searches recursively for JPG, JPEG, and PNG images.
# 2. Renames files by replacing spaces with underscores.
# 3. Converts JPG/JPEG images to PNG format.
# 4. Calculates image aspect ratio and width.
# 5. Identifies images matching a target screen aspect ratio.
# 6. Optionally copies matching images to a wallpaper collection folder.
#
# Matching Criteria:
# - Aspect ratio close to 16:9 portrait (0.5625)
# - Configurable tolerance range
# - Minimum image width requirement
#
# Requirements:
# - Bash
# - ImageMagick (identify, convert)
# - bc
#
# Usage:
#
# Scan only:
# ./collect-wallpapers.sh <image_directory>
#
# Scan and copy matching images:
# ./collect-wallpapers.sh <image_directory> -m
#
# Examples:
#
# Analyze images:
# ./collect-wallpapers.sh ~/Pictures
#
# Collect wallpaper candidates:
# ./collect-wallpapers.sh ~/Pictures -m
#
# Output:
# Matching files are copied to:
#
# /data/Ubuntu_Data/Videos/.stuff/
#
# Notes:
# - JPG/JPEG files are converted to PNG.
# - Spaces in filenames are replaced with underscores.
# - Original PNG files remain unchanged.
# - Original JPG/JPEG files are removed after conversion.
#
###############################################################################
if [ $# -lt 1 ]; then
echo "Usage: $0 <image_directory> [-m]"
exit 1
fi
IMG_PATH="$1"
MOVE=false
if [ "$2" == "-m" ]; then
MOVE=true
fi
echo "Path: $IMG_PATH"
find "$IMG_PATH" \( -name "*.png" -o -name "*.jp*g" \)
files="$(find "$IMG_PATH" \( -name "*.png" -o -name "*.jp*g" \) | wc -l)"
printf "%s Files, Move: %s\n" "$files" "$MOVE"
read -n 1 -s -r -p "Press any key to continue"
printf "\n"
minImgX="1400"
screenAR="0.5625"
AR_tolerance="0.2"
counter=0
find "$IMG_PATH" \( -name "*.png" -o -name "*.jp*g" \) | while read -r img; do
((counter++))
object_path="$(dirname "$img")/"
img="$(basename "$img")"
# Replace spaces with underscores
if [[ "$img" == *" "* ]]; then
new_img="$(echo "$img" | sed 's/ \+/_/g')"
mv "${object_path}${img}" "${object_path}${new_img}"
img="$new_img"
fi
# Convert JPG/JPEG to PNG
if echo "$img" | grep -qiE '\.jpe?g$'; then
convert "${object_path}${img}" \
"${object_path}$(echo "$img" | sed 's/\.[jJ][pP][eE]\?[gG]$/.png/')"
rm "${object_path}${img}"
img="$(echo "$img" | sed 's/\.[jJ][pP][eE]\?[gG]$/.png/')"
fi
# Get image dimensions
dimensions="$(identify -format "%wx%h" "${object_path}${img}" 2>/dev/null)"
width="${dimensions%x*}"
height="${dimensions#*x}"
if [[ -z "$width" || -z "$height" || "$height" -eq 0 ]]; then
continue
fi
# Calculate aspect ratio (height / width)
imageAR="$(echo "scale=6; $height / $width" | bc -l)"
# Match wallpaper criteria
if [[ "$MOVE" == true \
&& $(echo "$imageAR < $screenAR*(1+$AR_tolerance)" | bc -l) -eq 1 \
&& $(echo "$imageAR > $screenAR*(1-$AR_tolerance)" | bc -l) -eq 1 \
&& $(echo "$width > $minImgX" | bc -l) -eq 1 ]]; then
printf "%d/%s: %s\t%s\n" \
"$counter" \
"$files" \
"$img" \
"$imageAR"
cp "${object_path}${img}" \
"/data/Ubuntu_Data/Videos/.stuff/"
fi
done