如何将 ImageMagick 输出存储到 Bash 变量中(然后使用它)?
How to store ImageMagick output into Bash variable (and then use it)?
我使用 ImageMagick,需要根据条件调整图像大小。
为此,我将 identify
工具的结果存储到变量中。
$infile='test.jpg'
width=$(identify -ping -format %w $infile)
height=$(identify -ping -format %h $infile)
但在调整大小之前,我想做一些改变图像大小的转换:-trim
和 -shave
。所以我需要在 trim 调整和调整大小之间计算图像大小。我只想做一次 trim 操作来做一点优化。
那么,我想要:
- 去做trim和刮胡子
- store [binary]结果为一个变量(例如:
$data
)
- 将
$data
变量值作为输入传递给 identify
工具并存储其结果以进行条件大小调整
- 将
$data
传递给convert
工具并完成处理
像这样:
data=$(convert logo: -shave 1x1 gif:-)
width=$(echo $data | identify -ping -format %w gif:-)
echo $data | convert -resize "$width"
但是 echo
不能按需要工作。
P. S. convert
和 identify
是 tools from ImageMagick suite
Bash 无法存储可能包含 NULL
终止字符的数据块。但是您可以将数据转换为 base64, and use ImageMagick's fd:
协议。
# Store base64-ed image in `data'
data=$(convert logo: -shave 1x1 gif:- | base64)
# Pass ASCII data through decoding, and pipe to stdin file descriptor
width=$(base64 --decode <<< $data | identify -ping -format %w fd:0)
base64 --decode <<< $data | convert -resize "$width" -
我使用 ImageMagick,需要根据条件调整图像大小。
为此,我将 identify
工具的结果存储到变量中。
$infile='test.jpg'
width=$(identify -ping -format %w $infile)
height=$(identify -ping -format %h $infile)
但在调整大小之前,我想做一些改变图像大小的转换:-trim
和 -shave
。所以我需要在 trim 调整和调整大小之间计算图像大小。我只想做一次 trim 操作来做一点优化。
那么,我想要:
- 去做trim和刮胡子
- store [binary]结果为一个变量(例如:
$data
) - 将
$data
变量值作为输入传递给identify
工具并存储其结果以进行条件大小调整 - 将
$data
传递给convert
工具并完成处理
像这样:
data=$(convert logo: -shave 1x1 gif:-)
width=$(echo $data | identify -ping -format %w gif:-)
echo $data | convert -resize "$width"
但是 echo
不能按需要工作。
P. S. convert
和 identify
是 tools from ImageMagick suite
Bash 无法存储可能包含 NULL
终止字符的数据块。但是您可以将数据转换为 base64, and use ImageMagick's fd:
协议。
# Store base64-ed image in `data'
data=$(convert logo: -shave 1x1 gif:- | base64)
# Pass ASCII data through decoding, and pipe to stdin file descriptor
width=$(base64 --decode <<< $data | identify -ping -format %w fd:0)
base64 --decode <<< $data | convert -resize "$width" -