命令行自动裁剪图像?


18

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

有人有主意吗?

也许通过使用ImageMagick?

Answers:


38

(主要供个人将来参考),使用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
同样来自ImageMagick套件,mogrify将执行与convert相同的工作,但将覆盖原始副本而不是进行复制。
Yab

-transparent不适合我,但-trim有效。谢谢。
Ivan ZG 2012年

4

我已经有一段时间没有使用过了,但希望能有所帮助。制作一个gimp批处理脚本(我称为minecrop-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)
  )
)

然后保存此外壳脚本(例如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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.