82

I have a lot of images which have white borders around them. I would like to crop those borders all at once, preferably from command line. I believe that it can be done with ImageMagick, but I couldn't find suitable command.

I know that it can be done with Windows program named Irfanview with "Auto crop borders" option, but I am looking for Ubuntu-based and preferably command line based solution.

3
  • Have you tried Gimp?
    – Mitch
    Sep 30, 2013 at 7:46
  • 2
    @Mitch No, I didn't know that Gimp does have batch mode. Anyway, using ImageMagick Trim is simpler, so I will stay with that solution.
    – Rafal
    Sep 30, 2013 at 8:29
  • The dupe generally suggest GUI solutions...
    – Wilf
    Oct 9, 2015 at 11:35

1 Answer 1

116

ImageMagick Trim

The command line option trim used together with convert, or mogrify lets you trim borders of the same color as the corners of an image.

Usage:

convert input.png -trim output.png

The additional option -fuzz (which takes a percentage as an argument, e.g. -fuzz 10%) also removes colors near the corner colors.

Note: The -fuzz option must precede -trim because options' order matters for convert command to work as expected.

Use the option +repage to remove a canvas (if applicable).

ImageMagick Batch Trim (find)

Above command for ImageMagick Trim can also be used to batch process images combined with the find command:

find ./ -name "pattern" -exec convert {} -trim outputfolder/{} \;

The above command will trim all images that fit the pattern part of the command and save them in a new folder named outputfolder.

Assuming that images are PNGs, then command will look like this:

find ./ -name "*.png" -exec convert {} -trim outputfolder/{} \;

ImageMagick Batch Trim (mogrify)

While find allows for much greater control where output files will be placed, it is also possible to do the same with ImageMagick's mogrify:

mogrify -trim *.png

And if you want to crop colors near the corner colors (adjust the percentage based on the results you are observing):

mogrify -trim -fuzz 10% *.png

Please note that unlike convert and batch operation with find and convert, mogrify overwrites all files. To keep the originals use the -path option or do a backup copy of all images in the directory before proceeding with the mogrify command.

Side note: mogrify can be used to execute most (if not all) convert operations in batch, while overwriting original files.

As Trevor noted in the comments, you can use the -path option to output converted files to a new directory without overwriting the original files:

mogrify -trim -path trimmed_folder/ *.png

IrfanView

IrfanView runs quite nicely with Wine. Be sure to check the output of Irfanview carefully, as it sometimes breaks images when used with Wine.

12
  • It works. Thank you very much. I have added information about batch processing images with ImageMagick when combined find command as it was missing from your answer.
    – Rafal
    Sep 30, 2013 at 8:25
  • 10
    I used mogrify -trim *.jpg to crop all images in the current folder. Feb 5, 2014 at 6:42
  • 1
    This is great. It saved me a lot of time. TX :)
    – antmw1361
    Oct 16, 2014 at 4:08
  • 3
    is there any way to trim a png image along its height, keeping the width fixed?
    – ddas
    Aug 1, 2017 at 8:36
  • 1
    Note that if you don't want to overwrite the original files, you can use mogrify with the -path option: mogrify -trim -path trimmed_folder/ *.png
    – Trevor
    Mar 27, 2020 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .