Повысить резкость фотографий
Применяет unsharp mask к серии снимков с настройкой радиуса, силы и порога, сохраняя копии отдельно.
Что делает скрипт
Скрипт использует фильтр unsharp из ImageMagick, который усиливает контуры без грубого повышения контраста. Параметры позволяют отдельно управлять радиусом, сигмой, силой и порогом. Ориентация из EXIF применяется к пикселям, результаты пишутся в отдельную папку.
Быстрый старт
bash sharpen_photos.sh --dir ./photos --out ./sharp --amount 0.8
Скачайте файл, откройте терминал в папке со скриптом и выполните команду.
Другие варианты
bash sharpen_photos.sh --dir ./photos --out ./sharpbash sharpen_photos.sh --dir ./photos --out ./sharp --sigma 1.2 --amount 1.0 --threshold 0.03 --recursive
Что понадобится
sudo apt install bash
brew install bash
sudo apt install imagemagick
brew install imagemagick
sharpen_photos.sh
#!/usr/bin/env bash
set -euo pipefail
usage() {
echo "Использование: $0 --dir <папка> --out <папка> [--radius 0] [--sigma 1.0] [--amount 0.8] [--threshold 0.02] [--recursive] [--overwrite]"
}
die() {
echo "Ошибка: $*" >&2
exit 1
}
dir=""
out=""
radius=0
sigma=1.0
amount=0.8
threshold=0.02
recursive=0
overwrite=0
while [[ $# -gt 0 ]]; do
case "$1" in
--dir) dir="${2:-}"; shift 2 ;;
--out) out="${2:-}"; shift 2 ;;
--radius) radius="${2:-}"; shift 2 ;;
--sigma) sigma="${2:-}"; shift 2 ;;
--amount) amount="${2:-}"; shift 2 ;;
--threshold) threshold="${2:-}"; shift 2 ;;
--recursive) recursive=1; shift ;;
--overwrite) overwrite=1; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;;
esac
done
[[ -n "$dir" && -n "$out" ]] || { usage >&2; exit 2; }
[[ -d "$dir" ]] || die "папка не найдена: $dir"
for value in "$radius" "$sigma" "$amount" "$threshold"; do
[[ "$value" =~ ^[0-9]+([.][0-9]+)?$ ]] || die "числовой параметр задан неверно: $value"
done
if command -v magick >/dev/null 2>&1; then
CONVERT=(magick)
elif command -v convert >/dev/null 2>&1; then
CONVERT=(convert)
else
die "нужен ImageMagick: magick или convert"
fi
dir="${dir%/}"
mkdir -p "$out"
find_cmd=(find "$dir")
if [[ "$recursive" -eq 0 ]]; then
find_cmd+=(-maxdepth 1)
fi
find_cmd+=(-type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.webp' -o -iname '*.tif' -o -iname '*.tiff' \) -print0)
kernel="${radius}x${sigma}+${amount}+${threshold}"
done_count=0
skipped=0
failed=0
while IFS= read -r -d '' file; do
relative="${file#"$dir"/}"
target="$out/$relative"
mkdir -p "$(dirname "$target")"
if [[ -e "$target" && "$overwrite" -eq 0 ]]; then
echo "Пропуск: $target"
skipped=$((skipped + 1))
continue
fi
if "${CONVERT[@]}" "$file" -auto-orient -strip -unsharp "$kernel" -define png:compression-level=9 -quality 92 "$target"; then
echo "Готово: $relative"
done_count=$((done_count + 1))
else
rm -f -- "$target"
echo "Ошибка: $file" >&2
failed=$((failed + 1))
fi
done < <("${find_cmd[@]}")
echo "Итог: $done_count готово, $skipped пропущено, $failed ошибок"
[[ "$failed" -eq 0 ]]
Скрипты работают с файлами и настройками системы. Перед запуском прочитайте код и проверьте пути. Особенно если скрипт удаляет, перезаписывает или перемещает данные.