Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

By using Gimp's menu, you can automatically crop the image (removing white borders). I have a lot of images with white borders of different sizes. I want to remove them using Gimp in command line but I cannot figure out what the command is.

Anyone has an idea?

Maybe by using ImageMagick?

share|improve this question

3 Answers 3

up vote 15 down vote accepted

(Mainly for personal future reference,) using ImageMagick:

convert -trim image.jpg image.jpg

To trim/autocrop the whole directory:

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

Or using find:

find -name "*.jpg" -exec convert -trim "{}" "{}" \;
share|improve this answer
1  
Also from the ImageMagick suite, mogrify will do the same job as convert, but will overwrite the original instead of making copies. –  Yab May 16 '11 at 16:30
    
-transparent does not work for me, but -trim works. Thanks. –  Ivan Z. G. Xiao Mar 19 '12 at 22:42

I haven't used this in a while but hopefully it will help. Make a gimp batch script (I call mine crop-png.scm), and put it in ~/.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)
  )
)

Then save this shell scrip (e.g., pngcrop.sh) and call it on the png files like this: '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
share|improve this answer

Definitely ImageMagick.

share|improve this answer
5  
-1: Link-only posts are bad. –  Wuffers Mar 29 '11 at 21:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.