调整图像密度匹配指定打印尺寸的脚本异常问题排查
图像打印尺寸调整脚本异常排查
我编写了一段Bash脚本,用于复制输入图像并调整其密度,使副本能按指定英寸尺寸打印。例如,若有一张100x200像素的图像,想要使其适配2x3英寸的打印尺寸,只需指定outputPrintSize="2x3",并执行script.sh input.jpg output.jpg。
脚本代码
outputPrintSize="2x3" # 单位为英寸,示例:1x2、1x、x2 # 当前必须同时指定宽和高 IFS='x' read -rA ADDR <<< "$outputPrintSize" outputPrintW=${ADDR[1]} outputPrintH=${ADDR[2]} units=$(magick identify -format %U $1) if [[ $units = "Undefined" ]]; then input=$(mktemp ./$1~) cp $1 $input inputDensity=$( magick identify -format %xx%y $input ) magick $input -units PixelsPerInch -density $inputDensity $input else input=$1 fi inputPrintW=$(magick identify -format "%[fx:w/resolution.x]" $input) inputPrintH=$(magick identify -format "%[fx:h/resolution.y]" $input) if [[ $inputPrintW -gt $inputPrintH ]]; then inputDensityX=$(magick identify -format %x $input) # 公式参考:math.stackexchange.com/q/4936540 outputDensityX=$(( inputDensityX / outputPrintW * inputPrintW )) magick $input -units PixelsPerInch -density $outputDensityX $2 else inputDensityY=$(magick identify -format %y $input) outputDensityY=$(( inputDensityY / outputPrintH * inputPrintH )) magick $input -units PixelsPerInch -density $outputDensityY $2 fi if [[ $units = "Undefined" ]]; then rm $input; fi
问题描述
部分场景下输出文件的密度及打印尺寸不符合要求——当指定outputPrintSize="2x3"时,打印宽度不应超过2英寸,打印高度不应超过3英寸,但实际结果并非如此,烦请帮忙排查原因。
测试命令
magick identify -format "density: %xx%y-%U print size: %[fx:w/resolution.x]x%[fx:h/resolution.y]" $TestFile
测试结果
| 输入文件 | 输出文件 | 是否正常工作 |
|---|---|---|
| test1.jpg | test1_.jpg | 是 |
| 72x72-PixelsPerInch | 159x159-PixelsPerInch | |
| 4.44444x1.90278 | 2.01258x0.861635 | |
| ---------------- | ---------------- | -------------- |
| test2.jpg | test2_.jpg | 是 |
| 72x72-PixelsPerInch | 106x106-PixelsPerInch | |
| 1.90278x4.44444 | 1.29245x3.01887 | |
| ---------------- | ---------------- | -------------- |
| test3.png | test3_.png | 否 |
| 28.34x28.34-PixelsPerCm | 62.99x62.99-PixelsPerCm | |
| 11.2915x4.83416 | 5.08017x2.1749 | |
| ---------------- | ---------------- | -------------- |
| test4.png | test4_.png | 否 |
| 72x72-Undefined | 62.99x62.99-PixelsPerCm | |
| 3.2e+14x1.37e+14 | 5.08017x2.17495 | |
| ---------------- | ---------------- | -------------- |
| test5.jpg | test5_.jpg | 是 |
| 72x72-Undefined | 16x16-PixelsPerInch | |
| 4.8e+13x4.8e+13 | 3x3 |
使用版本
ImageMagick 7.1.1
内容的提问来源于stack exchange,提问作者user25418595
相关产品推荐
相关产品推荐

