如何为图像添加边框,使其大小可以被 4 整除
How to add borders to an image so that its size be divisible by 4
我尝试使用 ImageMagic 为图像添加边框,使其尺寸可以被 4 整除。我写道:
image_files=( "$alldir"/*.png )
current="${image_files[0]}"
page_height=$(identify -format "%h" $current)
page_width=$(identify -format "%w" $current)
border_x=$((4-(page_width%4) + 40))
border_y=$((4-(page_height%4) + 40))
使用 border
命令后,我会:
page_height= page_height + 2*border_y
page_width= page_width + 2*border_x
最后的page_height
和page_width
预计可以被4整除但是宽度是奇数。我的方法有什么问题?
我认为错误是将计算出的边框大小加倍。
说 page_width=1,然后 border_x=43,你最后的 page_width=1+2*43=87
如果你想要两边的边框都是 ~40,你可能需要以下内容:
border_x=$((4-(page_width%4) + 2*40))
border_y=$((4-(page_height%4) + 2*40))
page_height=$((page_height + border_y))
page_width=$((page_width + border_x))
与其计算每边边框的宽度并调整大小和填充,通常更容易计算边框图像的总大小、设置背景颜色并将图像的范围更改为新大小.所以,假设我从这张 181x149 像素的图像开始:
现在,我想要一个 40 像素的边框,所以我设置了一个 shell 变量:
border=40
然后我可以像这样在黄色背景上将图像居中:
magick input.png -background yellow -gravity center -extent "%[fx:w-w%4+2*$border]x%[fx:h-h%4+2*$border]" +repage result.png
如果您的 ImageMagick v6 版本不喜欢做数学运算,您可以让 shell 去做:
border=40
# Get width and height in one go with a bash "process substitution"
read w h < <(identify -format "%w %h" input.png)
convert input.png -background yellow -gravity center -extent "$((w-w%4+2*$border))x$((h-h%4+2*$border))" +repage result.png
我尝试使用 ImageMagic 为图像添加边框,使其尺寸可以被 4 整除。我写道:
image_files=( "$alldir"/*.png )
current="${image_files[0]}"
page_height=$(identify -format "%h" $current)
page_width=$(identify -format "%w" $current)
border_x=$((4-(page_width%4) + 40))
border_y=$((4-(page_height%4) + 40))
使用 border
命令后,我会:
page_height= page_height + 2*border_y
page_width= page_width + 2*border_x
最后的page_height
和page_width
预计可以被4整除但是宽度是奇数。我的方法有什么问题?
我认为错误是将计算出的边框大小加倍。 说 page_width=1,然后 border_x=43,你最后的 page_width=1+2*43=87
如果你想要两边的边框都是 ~40,你可能需要以下内容:
border_x=$((4-(page_width%4) + 2*40))
border_y=$((4-(page_height%4) + 2*40))
page_height=$((page_height + border_y))
page_width=$((page_width + border_x))
与其计算每边边框的宽度并调整大小和填充,通常更容易计算边框图像的总大小、设置背景颜色并将图像的范围更改为新大小.所以,假设我从这张 181x149 像素的图像开始:
现在,我想要一个 40 像素的边框,所以我设置了一个 shell 变量:
border=40
然后我可以像这样在黄色背景上将图像居中:
magick input.png -background yellow -gravity center -extent "%[fx:w-w%4+2*$border]x%[fx:h-h%4+2*$border]" +repage result.png
如果您的 ImageMagick v6 版本不喜欢做数学运算,您可以让 shell 去做:
border=40
# Get width and height in one go with a bash "process substitution"
read w h < <(identify -format "%w %h" input.png)
convert input.png -background yellow -gravity center -extent "$((w-w%4+2*$border))x$((h-h%4+2*$border))" +repage result.png