How to crop borders/white spaces from image?

82,201

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.

Share:
82,201

Related videos on Youtube

Rafal
Author by

Rafal

Updated on September 18, 2022

Comments

  • Rafal
    Rafal over 1 year

    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.

    • Mitch
      Mitch over 10 years
      Have you tried Gimp?
    • Rafal
      Rafal over 10 years
      @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.
    • Wilf
      Wilf over 8 years
      The dupe generally suggest GUI solutions...
  • Rafal
    Rafal over 10 years
    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.
  • Buttle Butkus
    Buttle Butkus over 10 years
    I used mogrify -trim *.jpg to crop all images in the current folder.
  • Rafal
    Rafal over 8 years
    @ButtleButkus I edited the answer to include that information. For some reason I forgot about this option earlier.
  • ddas
    ddas almost 7 years
    is there any way to trim a png image along its height, keeping the width fixed?
  • Prof.Chaos
    Prof.Chaos over 5 years
    I think it's noteworthy that mogrify will modify the existing files (rather than creating new copies as pdfcrop would do). After all, you might loose "data"...
  • Prof.Chaos
    Prof.Chaos over 5 years
    Well, I wasn't reading the entire post, but just the comments. I was hence referring to Buttle Butkus' comment. And since I was sure that others might do that, too, I wanted to add that note close to it, too.
  • 00-BBB
    00-BBB over 4 years
    the trim find command gives me this output: convert: WriteBlob Failed `outputfolder/.//outputfolder/filename.png' @ error/png.c/MagickPNGErrorHandler/1715.
  • 00-BBB
    00-BBB over 4 years
    Regardless of the above error output, the trimmed images are actually there. Tried to get it to trim top and bottom only, ended up giving up and using Photoshop... (almost got it working, just kept adding white lines down the middle whenever i 'batched' the command):
  • Takkat
    Takkat over 4 years
    @00-BBB: for a scriptable method to trim only one side see imagemagick.org/Usage/crop/#trim_oneside
  • Trevor
    Trevor about 4 years
    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
  • Takkat
    Takkat about 4 years
    @wxyz: thank you - included a note on this in my answer.