更改密度,以便以指定的打印尺寸(以英寸为单位)打印图像

问题描述 投票:0回答:1

以下脚本创建输入图像的副本并更改其密度,以便以指定的打印尺寸(以英寸为单位)打印副本。例如,如果您有一个 100x200 像素的图像,并且希望打印它以适合 2x3 英寸的盒子,则需要指定

outputPrintSize="2x3"
,然后运行
script.sh input.jpg output.jpg

outputPrintSize="2x3" # In inches. E. g., 1x2, 1x, or x2.
                      # Currently, both width and height must be specified

outputPrintW=$(echo "$outputPrintSize" | awk -F 'x' '{print $1}')
outputPrintH=$(echo "$outputPrintSize" | awk -F 'x' '{print $2}')

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)
  # https://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\nprint size: %[fx:w/resolution.x]x%[fx:h/resolution.y]" $TestFile

结果:

| input file              | output file             | works correctly? |
|-------------------------|-------------------------|------------------|
| test1.jpg               | test1_.jpg              | yes              |
| 72x72-PixelsPerInch     | 159x159-PixelsPerInch   |                  |
| 4.44444x1.90278         | 2.01258x0.861635        |                  |
|- - - - - - - - - - - - -| - - - - - - - - - - - - |- - - - - - - - - |
| test2.jpg               | test2_.jpg              | yes              |
| 72x72-PixelsPerInch     | 106x106-PixelsPerInch   |                  |
| 1.90278x4.44444         | 1.29245x3.01887         |                  |
|- - - - - - - - - - - - -| - - - - - - - - - - - - |- - - - - - - - - |
| test3.png               | test3_.png              | no               |
| 28.34x28.34-PixelsPerCm | 62.99x62.99-PixelsPerCm |                  |
| 11.2915x4.83416         | 5.08017x2.1749          |                  |
|- - - - - - - - - - - - -| - - - - - - - - - - - - |- - - - - - - - - |
| test4.png               | test4_.png              | no               |
| 72x72-Undefined         | 62.99x62.99-PixelsPerCm |                  |
| 3.2e+14x1.37e+14        | 5.08017x2.17495         |                  |
|- - - - - - - - - - - - -| - - - - - - - - - - - - |- - - - - - - - - |
| test5.jpg               | test5_.jpg              | yes              |
| 72x72-Undefined         | 16x16-PixelsPerInch     |                  |
| 4.8e+13x4.8e+13         | 3x3                     |                  |
|- - - - - - - - - - - - -| - - - - - - - - - - - - |- - - - - - - - - |

测试文件:https://github.com/jsx97/test/blob/main/密度.zip

imagemagick
1个回答
0
投票

对于我来说,这在 Imagemagick 中对于输入的大小似乎是一致的。我添加了计算打印尺寸(以厘米为单位)的替代方法。如果你想将其转换为英寸,则除以 2.54

以厘米为单位:

magick identify -format "size: %wx%h\ndensity: %xx%y\ndensity: %[resolution.x]x%[resolution.y]\nunits: %U\nprint size (cm): %[fx:w/resolution.x]x%[fx:h/resolution.y]\nprint size (cm): %[printsize.x]x%[printsize.y]\n" test3.png

size: 320x137
density: 28.339999999999999858x28.339999999999999858
density: 28.34x28.34
units: PixelsPerCentimeter
print size (cm): 11.2915x4.83416
print size (cm): 11.2915x4.83416

以英寸为单位:

magick identify -format "size: %wx%h\ndensity: %xx%y\ndensity: %[resolution.x]x%[resolution.y]\nunits: %U\nprint size (in): %[fx:(w/resolution.x)/2.54]x%[fx:(h/resolution.y)/2.54]\nprint size (in): %[fx:printsize.x/2.54]x%[fx:printsize.y/2.54]\n" test3.png

size: 320x137
density: 28.339999999999999858x28.339999999999999858
density: 28.34x28.34
units: PixelsPerCentimeter
print size (in): 4.44546x1.90321
print size (in): 4.44546x1.90321
© www.soinside.com 2019 - 2024. All rights reserved.