如何在不裁剪的情况下将一批矩形图像转换为方形图像?

How to convert batch of rectangular images into square images without cropping?

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

rect-h.png

rect-v.png

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

所以为了获得相同的尺寸,not-cutted,没有变形:

rect-h-sq.png

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。

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