Imagemagick - Remove Black Borders

Imagemagick - Remove Black Borders

I have about 100 scanned pictures (.jpg), all with black borders (top/right/bottom/left), like this:

input.jpg

I want to trim my picture automatically, that it is without ANY black border.

I tried different codes, the best result I get with this code (I use Imagemagick Version 7.0.7-Q16 for Windows):

magick mogrify -bordercolor black -fuzz 20% -trim -format jpg *.jpg

It generates this picture:

result with my code

Which is a good start, but as you can still see there is a rest of the black border on the top, right, bottom and left side of the image and two black areas in the sky .

I want that the picture looks like this:

wish result

How can I do this?

Any help or hint for my problem will be appreciated! Thanks in advance!

2 Answers

In ImageMagick 7.0.8.30 or higher you can now do an aggressive trim to get rid of (most) of the black. For one image, I would normally use just magick, but for demonstration purposes, here, I use magick mogrify.

Input:

magick mogrify -fuzz 4% -define trim:percent-background=0% -trim +repage -format jpg img.jpg

Adjust the fuzz value as appropriate. But if you make it too large, your image will be trimmed too much.

see

You cannot always get rid of all the black background by that command alone, especially if the image is slightly rotated. You may need to shave some off all around, but there is no easy way to know by how much. Also the background color you specify has no effect in your ImageMagick trim command.

Try

magick mogrify -fuzz 20% -trim +repage -shave 7x7 -format jpg *.jpg

Adjust the shave amounts as desired.

0

Your Answer

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

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.