如何将一批矩形图像转换成方形图像而不进行裁剪?

时间:2018-03-29 12:48:51

标签: imagemagick image-resizing imagemagick-convert

给定一批垂直和水平矩形图像:

enter image description here rect-h.png

enter image description here rect-v.png

如何将一批垂直和水平矩形图像转换为方形图像?

所以要获得相同的尺寸,不要切割,不要变形:

enter image description here rect-h-sq.png

enter image description here rect-v-sq.png

我目前正在使用

mkdir -p ./temp ./png                                  # create folders to work on copies of data and store final png output
cp ./* ./temp                                          # copies to ./temp, so to word on copies 
for file in ./temp/*.png                               # loop on the [edited] copies in ./temp
do
  keyIn=$(basename "$file" .png)                       # name of the file minus .png
  keyOut=$(basename "$file" .png)-sq.png               # name of the file minus .png, plus .-red.png 
  convert -background none -density 1200 ./temp/$keyIn.png -resize 300x300\! ./png/$keyOut   
done

但它失败了。

注意:密度是因为我经常使用svg。

1 个答案:

答案 0 :(得分:1)

Padding有效:

mkdir -p ./temp ./png                                  # create folders to work on copies of data and store final png output
cp ./* ./temp                                          # copies to ./temp, so to word on copies 
for file in ./temp/*.png                               # loop on the [edited] copies in ./temp
do
  keyIn=$(basename "$file" .png)                       # name of the file minus .png
  keyOut=$(basename "$file" .png)-sq.png               # name of the file minus .png, plus .-red.png 
  convert -background none -density 1200 ./temp/$keyIn.png \
     -thumbnail '300x300>' -background white \
     -gravity center -extent 300x300 -resize 300x300\! ./png/$keyOut   
done