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
  • 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 '13 at 8:29
  • The dupe generally suggest GUI solutions...
    – Wilf
    Oct 9 '15 at 11:35

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.


convert input.png -trim output.png

The additional options -fuzz (which takes a percentage as an argument, e.g. -fuzz 10%) also removes colors near the corner colors. 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 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

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 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 -path option to output converted files to a new directory without overwriting original files:

mogrify -trim -path trimmed_folder/ *.png


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
  • 10
    I used mogrify -trim *.jpg to crop all images in the current folder. 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
  • 3
    is there any way to trim a png image along its height, keeping the width fixed?
    – ddas
    Aug 1 '17 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 '20 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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