Автор: arbtttrn6
Используется ImageMagick. Можно создать сценарий, который будет содержать следующую команду:
#!/bin/bash
#arbtttrn6 2025
input_files=(*.jpg)
output_dir="./bmpoj"
mkdir -p "$output_dir"
round_to_power() {
local num=$1
local candidates=(16 32 64 128 256)
local nearest=${candidates[0]}
local min_diff=$(( num - ${candidates[0]} ))
min_diff=${min_diff#-}
for c in "${candidates[@]}"; do
diff=$(( num - c ))
diff=${diff#-}
if [ $diff -lt $min_diff ]; then
min_diff=$diff
nearest=$c
fi
done
echo "$nearest"
}
for file in "${input_files[@]}"; do
width=$(identify -format "%w" "$file")
height=$(identify -format "%h" "$file")
ratio=$(echo "scale=2; $width / $height" | bc)
if (( $(echo "$ratio >= 1" | bc -l) )); then
new_width=256
new_height=$(echo "scale=2; 256 / $ratio" | bc)
else
new_height=256
new_width=$(echo "scale=2; 256 * $ratio" | bc)
fi
new_width_int=$(LC_NUMERIC=C printf "%.0f" "$new_width")
new_height_int=$(LC_NUMERIC=C printf "%.0f" "$new_height")
target_width=$(round_to_power $new_width_int)
target_height=$(round_to_power $new_height_int)
convert "$file" \
-resize "${target_width}x${target_height}!" \
-compress none \
-format bmp \
-define bmp:format=bmp3 \
-colors 256 \
"${output_dir}/${file%.jpg}.bmp"
done
Здесь результат будет отправляться в папку "bmpoj" (-path ./bmpoj), размер изображений будет устанавливаться в зависимости от соотношения сторон, сжатия нет (-compress none), формат bmp (-format bmp -define bmp:format=bmp3), цветов 256 (-colors 256), а в качестве входных изображений идут изображения формата jpg ("*.jpg").