How to crop borders/white spaces from image?
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.