【问题标题】:Add padding to an image to make it square using ImageMagick使用 ImageMagick 向图像添加填充以使其成为正方形
【发布时间】:2016-09-29 15:31:59
【问题描述】:

我收到了一个文件夹,里面装满了白色背景的产品图片,并要求我把它们做成正方形,并且在产品周围都有一致的填充量。

我想避免调整图像本身的大小,所以我的计划是修剪图像,添加填充使图像呈方形,然后为图像添加边框,作为结果图像的百分比。

我唯一不知道的部分是如何进行填充。我见过的所有平方图像的例子都在这个过程中调整了图像的大小。有没有办法找到最长的大小并据此调整大小?

示例: 我有以下图像:

我需要它看起来像这样:

我还没有 ImageMagick 的代码(除了简单的修剪和边框命令)。这个想法是修剪空白,然后使图像成为正方形,将产品(在本例中为矩形黑框)留在图像的中心。最后,添加边框为图像提供填充。

【问题讨论】:

  • 请给出一个图像的具体例子,以及它应该如何被平方和填充——还有你到目前为止有什么代码
  • 我添加了一个示例图像和我想要的输出。
  • 所以您的裁剪图像是 629x157 - 对吗?你怎么知道它应该变成 758x758?
  • 我使用了计算器。在 ImageMagick 中,我将添加 20% 的边框。

标签: imagemagick imagemagick-convert


【解决方案1】:

我认为是这样的:

#!/bin/bash
# Get trim box w, h, x, y
IFS=" x+" read w h x y < <(convert -fuzz 10% start.jpg -format "%@" info:)

# Get longest side
longest=$w
[ $h -gt $longest ] && longest=$h

# Increase by 20%
longest=$(echo "scale=0;$longest*1.2/1" | bc)
echo $longest

convert -fuzz 10% start.jpg -trim -background white -gravity center -extent ${longest}x${longest} result.jpg

如果你对"bashisms"不是很熟悉,你可以运行

convert -fuzz 10% start.jpg -format "%@" info:

查看第一个命令在做什么 - 它只是获取修剪框而没有实际修剪。尝试在其后添加这一行:

echo $w, $h, $x, $y

【讨论】:

  • 谢谢,这看起来可行。我将尝试对其进行一些调整,以便可以在图像目录上运行它。我在定义我的范围大小时尝试过 fx:max(w,h) 但这不起作用。我不知道为什么。
  • 使用此方法将中断 for 循环内部。我发现获取宽度的另一种方法是:w=identify -format '$w' $i
【解决方案2】:

以 Linux 作为 shell 脚本:

for file in *\.png ;  # or jpg,....
    do  IMG_W=$(identify -ping -format '%w' $file); 
    IMG_H=$(identify -ping -format '%h' $file); 
##   echo "$file: w: $IMG_W x h $IMG_H"; 
##   do something like
##   DIN A5: 2331x3307 pixel 400 dpi
##   half the width of white border
#   XPLUS=$(echo " ( 2331 - $IMG_W ) / 2 " |bc) ; 
#   YPLUS=250                                     
##   250 pixel from top border
##   echo "position: Xplus $XPLUS Yplus $YPLUS";
##   according to the calculations combine with white picture (background)
#   composite -geometry +$XPLUS+$YPLUS $file white.png ../tmp7/$file;
done

该示例(已注释掉)向您展示了如何计算并与文件合并。我不知道我从哪个来源获得了上面的两行脚本(这是我编写的脚本的一部分),我想来自 Anthony Thyssen 在他的优秀网站“ImageMagick 使用示例”上。

【讨论】:

    【解决方案3】:

    这段代码来自 Mark Setchell 对这个问题的回答,用 fish shell 脚本重写。我使用他的解决方案在图像周围添加填充以使其成为正方形。

    #!/usr/bin/fish
    convert -fuzz 10% $argv -format "%@" info: \
              | string split x \
              | string split + \
              | read -L w h x y
    
    set longest $w
    [ $h -gt $longest ] && set longest $h
    set longest (math -s0 $longest\*1.2)
    
    set output_file result-000.png
    [ -f $output_file ] \
      && echo "file $output_file already exists" \
      || echo "file $output_file will be created" 
    [ -f $output_file ] \
      || convert -fuzz 10% $argv -trim -background white -gravity center -extent {$longest}x{$longest} $output_file
    

    另存为文件,例如frame.fish,赋予其执行权限,chmod +x frame.fish,并使用文件./frame.fish imageinput.png运行。

    string split 将被每个分隔符解析成不同的行。 read -L 将读入每一行。 math -s0 在鱼壳中生成零比例小数。

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      相关资源
      最近更新 更多