Automatically trim/remove white/black/any borders from images


/ Published in: DOS Batch
Save to your folder(s)

Worked great for trimming margins in scanned comics and books, much easier and faster than complex photoshop scripts. :)

1. Download & install ImageMagick from http://www.imagemagick.org/script/binary-releases.php#windows.
This is a command line utility to work with images. No ads, toolbars and other crap. It is freeware and open source.
You need file named ImageMagick--windows-static.exe

2. Create new file conv.bat with this snippet inside in a folder with images and launch it.
This will automatically trim all images in this folder and all its subfolders.

3. Optionally you can set different extension if you need to crop something other than jpegs's, and increase color tolerance - to detect larger borders. There are also other settings, like cropping only borders of certain color - you can read full documentation at http://www.imagemagick.org/Usage/crop/#trim .

It will also work on Linux and Mac since ImageMagick is a crossplatform tool, but you will have to modify .bat file to be compatible with Bash syntax.


Copy this code and paste it in your HTML
  1. @echo off
  2.  
  3. rem 1. Download & install ImageMagick from http://www.imagemagick.org/script/binary-releases.php#windows.
  4. rem This is a command line utility to work with images. No ads, toolbars and other crap. It is freeware and open source.
  5. rem You need file named ImageMagick-<version number>-windows-static.exe
  6. rem
  7. rem 2. Create new file conv.bat with this snippet inside in a folder with images.
  8. rem This will automatically trim all images in this folder and all its subfolders.
  9.  
  10. rem Settings :
  11.  
  12. rem Images extension
  13. rem jp*g will match both .jpg's and .jpeg's
  14. set extension=jp*g
  15.  
  16. rem How much tolerance when matching color in percents
  17. rem Larger tolerance = broader color range is regarded same and trimmed
  18. rem Zero will mach only one exact color, use it for perfectly clean images in lossless formats (png, tif etc)
  19. rem Use small value for clean images in jpegs, 1%%-5%% is a good start
  20. set tolerance=4%%
  21.  
  22. rem More info on parameters - see http://www.imagemagick.org/Usage/crop/#trim
  23.  
  24. for /R %%i in (*.%extension%) do (
  25. echo Trimming %%i
  26. convert "%%i" -fuzz "%tolerance%" -trim +repage "%%i"
  27. )

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.