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.

  • Have you tried Gimp? – Mitch Sep 30 '13 at 7:46
  • 1
    @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 '13 at 8:29
  • The dupe generally suggest GUI solutions... – Wilf Oct 9 '15 at 11:35
up vote 53 down vote accepted

ImageMagick Trim

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

Usage:

convert input.png -trim output.png

The additional options -fuzz also removes colors near the corner colors. Use option +repage to remove a canvas (if applicable).

ImageMagick Batch Trim (find)

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

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

will trim all images that fits pattern part of command and save them in 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

Please note that unlike convert and batch operation with find and convert mogrify overwrites all files. It is best to do a backup copy of all images in the directory before proceeding with mogrify command.

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

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.

  • 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 '13 at 8:25
  • 8
    I used mogrify -trim *.jpg to crop all images in the current folder. – Buttle Butkus Feb 5 '14 at 6:42
  • 1
    This is great. It saved me a lot of time. TX :) – antmw1361 Oct 16 '14 at 4:08
  • @ButtleButkus I edited the answer to include that information. For some reason I forgot about this option earlier. – Rafal Oct 9 '15 at 10:17
  • 1
    is there any way to trim a png image along its height, keeping the width fixed? – ddas Aug 1 '17 at 8:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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