This commit is contained in:
jingyun
2025-08-22 15:13:34 +08:00
parent 961b2ae1ee
commit 1c07d576d3
44 changed files with 8049 additions and 16 deletions

114
examples/assets/upscale_to_8k.sh Executable file
View File

@@ -0,0 +1,114 @@
#!/bin/zsh
setopt null_glob
# 设置默认值或从环境变量获取
INPUT="${INPUT_DIR:-./images}"
OUTPUT="${OUTPUT_DIR:-./images_output}"
MODELPATH="${MODELPATH:-./models}"
MODELNAME="${MODELNAME:-ultramix-balanced-4x}"
BIN_PATH="${BIN_PATH:-./bin/mac/upscayl-bin}"
TARGET_WIDTH=4320
TARGET_HEIGHT=7680
FORMAT="jpg"
THREADS="4:8:4"
TARGET_SIZE=$((10 * 1024 * 1024)) # 10MB
MAX_SIZE=$((20 * 1024 * 1024)) # 20MB
echo "===== 开始批量处理图片 ====="
echo "输入目录: $INPUT"
echo "输出目录: $OUTPUT"
echo "模型路径: $MODELPATH"
echo "模型名称: $MODELNAME"
echo "二进制文件: $BIN_PATH"
# 确保输出目录存在
mkdir -p "$OUTPUT"
# 循环对每个图片单独执行 upscayl-bin输出路径为文件路径
for img in "$INPUT"/*.{png,jpg,jpeg}; do
[ -e "$img" ] || continue
base=$(basename "$img")
out_img="$OUTPUT/${base%.*}.$FORMAT"
compress=10 # 0为无压缩100为最大压缩
try=1
echo "\n==== 开始处理: $base ===="
echo "输入文件: $img"
echo "输出文件: $out_img"
echo "模型: $MODELNAME, 模型路径: $MODELPATH"
echo "格式: $FORMAT"
# 获取原图宽高
read orig_w orig_h <<< $(sips -g pixelWidth -g pixelHeight "$img" 2>/dev/null | awk '/pixelWidth/ {w=$2} /pixelHeight/ {h=$2} END{print w,h}')
if [ "$orig_w" -lt "$orig_h" ]; then
short=$orig_w
long=$orig_h
else
short=$orig_h
long=$orig_w
fi
echo "DEBUG: orig_w=$orig_w, orig_h=$orig_h, short=$short, long=$long"
scale1=$(echo "scale=8; 4320/$short" | bc)
scale2=$(echo "scale=8; 7680/$long" | bc)
echo "DEBUG: scale1=$scale1, scale2=$scale2"
# 取scale1和scale2的最大值
scale=$(echo "$scale1 > $scale2" | bc -l)
if [ "$scale" -eq 1 ]; then
use_scale=$scale1
else
use_scale=$scale2
fi
use_scale=$(printf "%f" "$use_scale")
echo "DEBUG: use_scale=$use_scale"
# 保证不缩小
scale_cmp=$(echo "$use_scale > 1" | bc -l)
echo "DEBUG: scale_cmp=$scale_cmp"
if [ "$scale_cmp" -eq 1 ]; then
target_w=$(awk -v w="$orig_w" -v s="$use_scale" 'BEGIN{printf "%d", w*s+0.5}')
target_h=$(awk -v h="$orig_h" -v s="$use_scale" 'BEGIN{printf "%d", h*s+0.5}')
echo "原图分辨率: ${orig_w}x${orig_h}, 目标分辨率: ${target_w}x${target_h} (短边≥4320, 长边≥7680)"
else
target_w=$orig_w
target_h=$orig_h
echo "原图分辨率: ${orig_w}x${orig_h}, 已满足短边≥4320且长边≥7680无需放大"
fi
while true; do
echo "尝试第 $try 次: compress=$compress"
upscale_cmd="\"$BIN_PATH\" -i \"$img\" -o \"$out_img\" -f $FORMAT -m \"$MODELPATH\" -n $MODELNAME -r \"${target_w}x${target_h}\" -c $compress -j $THREADS -v"
echo "执行命令: $upscale_cmd"
eval $upscale_cmd
if [ ! -f "$out_img" ]; then
echo "$base: 未生成图片compress=$compress"
break
fi
size=$(stat -f%z "$out_img")
res=$(sips -g pixelWidth -g pixelHeight "$out_img" 2>/dev/null | awk '/pixelWidth|pixelHeight/ {print $2}' | xargs | sed 's/ /x/')
echo "$base: compress=$compress, size=${size} bytes, 分辨率=${res}"
if [ $size -le $MAX_SIZE ] || [ $compress -ge 100 ]; then
echo "$base: 满足大小要求,最终 compress=$compress"
break
fi
# compress 每次递增10最大不超过100
compress=$((compress + 10))
[ $compress -gt 100 ] && compress=100
try=$((try+1))
[ $try -gt 10 ] && echo "$base: 达到最大尝试次数,停止调整" && break
done
done
echo "\n===== 输入目录: $INPUT ====="
for img in "$INPUT"/*.{png,jpg,jpeg}; do
[ -e "$img" ] || continue
size=$(stat -f%z "$img")
res=$(sips -g pixelWidth -g pixelHeight "$img" 2>/dev/null | awk '/pixelWidth|pixelHeight/ {print $2}' | xargs | sed 's/ /x/')
echo "$(basename "$img"): ${size} bytes, ${res}"
done
echo "\n===== 输出目录: $OUTPUT ====="
for img in "$OUTPUT"/*.{png,jpg,jpeg}; do
[ -e "$img" ] || continue
size=$(stat -f%z "$img")
res=$(sips -g pixelWidth -g pixelHeight "$img" 2>/dev/null | awk '/pixelWidth|pixelHeight/ {print $2}' | xargs | sed 's/ /x/')
echo "$(basename "$img"): ${size} bytes, ${res}"
done