命令行自动裁剪图像?

命令行自动裁剪图像?

通过使用 Gimp 的菜单,您可以自动裁剪图像(去除白色边框)。我有很多不同大小的白色边框的图像。我想在命令行中使用 Gimp 删除它们,但我不知道命令是什么。

有人有想法吗?

也许可以使用 ImageMagick?

答案1

通过使用 ImageMagick:

convert -trim image.jpg image.jpg

要修剪/自动裁剪整个目录:

for a in *.jpg; do convert -trim "$a" "$a"; done

或者使用find

find -name "*.jpg" -exec convert -trim "{}" "{}" \;

答案2

我已经有一段时间没用过它了,但希望它能有所帮助。制作一个 gimp 批处理脚本(我将其命名为 crop-png.scm),并将其放在 ~/.gimp-2.6/scripts/ 中。

(define (crop-png filename)
  (let* 
    (
    (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
    (drawable (car (gimp-image-get-active-layer image)))
    )

  ; crop the image
  (plug-in-autocrop RUN-NONINTERACTIVE image drawable)

  ; save in original png format
  (file-png-save RUN-NONINTERACTIVE image drawable filename filename
       0 6 0 0 0 1 1)

  ; clean up the image
  (gimp-image-delete image)
  )
)

然后保存此 shell 脚本(例如 pngcrop.sh)并在 png 文件上调用它,如下所示:'pngcrop.sh *.png'

#!/bin/bash

if [ $# -le 0 ]; then
    echo
    echo "Usage: $(basename $0) file1.png [file2.png ...]"
    echo
    echo "  This script uses gimp to autocrop PNG files and"
    echo "  save them to PNG format.  You must have"
    echo "  crop-png.scm installed in your gimp "
    echo "  scripts directory."
    echo
    exit 1
fi

# set the filelist
files=$*

# # set the base command
# CMD="gimp -i -b "

# loop and add each file
for i in ${files[*]} ; do
  # #echo $i
  # ARGS="\"(crop-png \\\"$i\\\")\""
  # CMD="$CMD $ARGS"

  gimp -i -b "(crop-png \"$i\")" -b "(gimp-quit 0)"
done

# # add the end to quit
# TAIL="-b \"(gimp-quit 0)\""
# CMD="$CMD $TAIL"
# 
# #echo $CMD
# eval $CMD

相关内容