3

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!

TimBuktu
  • 3
  • 1
  • 2
SueSchi
  • 51
  • 1
  • 4

3 Answers3

6

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:

enter image description here

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

enter image description here

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

see https://imagemagick.org/discourse-server/viewtopic.php?f=4&t=35579

fmw42
  • 46,825
  • 10
  • 62
  • 80
2

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.

fmw42
  • 46,825
  • 10
  • 62
  • 80
0

Another possibility is to use floodfill, e.g.:

convert sample-in.jpg \
    -alpha set -fill none -fuzz 5% \
    -draw "color 0,0 floodfill" \
    sample-out.webp
  • enable transparency
  • floodfill makes pixels transparent where pixels are within 5% of black, starting flood from 0,0
Alex
  • 2,784
  • 2
  • 32
  • 46