用于对多个图像进行平方的 gimp 脚本,保持纵横比和最小尺寸

gimp script to square multiple images, maintaining aspect ratio and minimum dimensions

我有数百个不同大小的 jpg(例如 2304px x 2323px)。

在 gimp 中,我可以使用批处理过滤器将它们更改为特定大小,相对大小或绝对大小。但是对于某些配置,我必须手动执行以下操作,这对于所有图像都需要永远:

  1. 将最短边的大小更改为 500 像素,保持纵横比,使较长的边至少为 500 像素。因此,如果图像是 1000 x 1200,现在将是 500 x 600。图像有纵向和横向。
  2. 更改 canvas 大小,使图像成为 500 像素 x 500 像素的正方形,居中。这将切掉部分图像(这很好,无论如何大多数图像几乎都是正方形的)。
  3. 导出文件名后附加 -s 的文件。

是否有可用于自动执行这些步骤的脚本?

除非您找到了使用 gimp 的方式,否则您可能需要查看 ImageMagickmogrify 工具允许修改和调整图像大小。

注意:mogrify 将覆盖您的文件,除非您使用 stdin/stdout。您可能需要这样的脚本:

#!/bin/sh
for image in `ls /your/path/*jpg`; do
    mogrify -resize ... - < "$image" > "${image%%.png}-s.jpg"
done

ImageMagick 中的类似内容。它并不像看起来那么难,因为大部分都是评论。尝试 COPY 您的文件 - 它会执行当前目录中的所有 JPEG。

#!/bin/bash

shopt -s nullglob
shopt -s nocaseglob

for f in *.jpg; do
   echo Processing $f...

   # Get width and height
   read w h < <(convert "$f" -format "%w %h" info: )
   echo Width: $w, Height: $h

   # Determine new name, by stripping extension and adding "s"
   new=${f%.*}
   new="${new}-s.jpg"
   echo New name: $new

   # Determine lesser of width and height
   if [ $w -lt $h ]; then
      geom="500x"
   else
      geom="x500"
   fi
   convert "$f" -resize $geom -gravity center -crop 500x500+0+0! "$new"
done