代码之家  ›  专栏  ›  技术社区  ›  Hugo LOPEZ

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

  •  0
  • Hugo LOPEZ  · 技术社区  · 7 年前

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

    enter image description here rect-h.png

    enter image description here 矩形-v.png

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

    因此,要获得相同的尺寸,不切割,不变形:

    enter image description here 矩形-h-sq.png

    enter image description here 矩形-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 回复  |  直到 7 年前
        1
  •  2
  •   Hugo LOPEZ    7 年前

    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