CROP black from scanned images

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
markosjal
Posts: 7
Joined: 2018-04-20T13:15:16-07:00
Authentication code: 1152

CROP black from scanned images

Post by markosjal »

I have some scanned images that sometimes have black (varying black not all pure black) on sides and top/bottom of image.
I want to remove the black from sides and top/bottom and CROP where the black was .

I do not want to fill the black space with white I want to CROP IT OFF

I have tried numerous examples here but I usually get a result that I did not anticipate.

I believe I need to use the "-blur" option to compensate for the varying black on sides and top/bottom as there are usually vertical lines with varying shades of black

It might be nice to deskew at the same time as an option


Does anyone have an example that does this ?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: CROP black from scanned images

Post by Bonzo »

I recommend you read this then repost: viewtopic.php?f=1&t=9620
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CROP black from scanned images

Post by fmw42 »

What is your Imagemagick version and platform? Please always provide that. Can you post an image to some free hosting service and put the URL here so we can see an example of your problem.
markosjal
Posts: 7
Joined: 2018-04-20T13:15:16-07:00
Authentication code: 1152

Re: CROP black from scanned images

Post by markosjal »

Here is the image. I am using ubuntu 16.04 and the version of imagemagic from ubuntu repos . If it is that specific to versions I may have to return to my old idea and not use imagaemagic, which I did not want to use anyway. This is something to be installed on varios distros and I can not afford to battle with imagemagic versions on every install


http://teknogeekz.com/Scan1524186434.jpg

Image

you can see the black at top, bottom and sides of this image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CROP black from scanned images

Post by fmw42 »

You would normally be able to use -fuzz XX% -trim +repage to do what you want with a 10-25% fuzz value. But you have a partial gray vertical stripe at about x=350 that causes problems. But I have increased the XX=70% and that seems to work. But it is possible with such a large value that other images may get cropped too much. You should check your scanning system and see if you can prevent or remove the gray stripe.

Try this:

Code: Select all

convert Scan1524186434.jpg -fuzz 70% -trim +repage result.jpg
see https://www.imagemagick.org/Usage/crop/#trim
markosjal
Posts: 7
Joined: 2018-04-20T13:15:16-07:00
Authentication code: 1152

Re: CROP black from scanned images

Post by markosjal »

It does not work at all here I get an uncropped image . I can see that the new file is generated with the current time.

this is an echo of the php command for this file:
convert filemanager/userfiles/Scan1524186434.jpg -fuzz 90% -trim +repage filemanager/userfiles/Scan1524186434Crop.jpg



I am thinking as I thought in the beginning imagemagick always seems problematic and there must be another way.

thanks anyhow. I do appreciate it
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CROP black from scanned images

Post by fmw42 »

Imagemagick is not problematic per se. Images can be problematic. The may be no universal solution to any given problem for all images.

My previous command works fine for me. -fuzz 90% may be way too large.

The issue may be your version of Imagemagick or your delegate libraries for libjpeg.

Did you read the earlier posts and read viewtopic.php?f=1&t=9620

What is your IM version and platform? Always provide that?

What do you get from

Code: Select all

convert -version
Are you on a current version of Imagemagick or a very old one?

What do you get from

Code: Select all

convert -list format
for the line starting with JPG?

Are you sure of your file paths? Do those directories have adequate permissions?
markosjal
Posts: 7
Joined: 2018-04-20T13:15:16-07:00
Authentication code: 1152

Re: CROP black from scanned images

Post by markosjal »

I tried 70% too

I guess imagemagick is just too inconsistent from distro to distro for my app. I will have to look for a different solution or forget the auto-crop option alltogether.

As this is an App specific to a scanner and the auto-crop feature compensates for some firmware versions of the scanners that do not crop when scanning, ensuring the version of imagemagick is impossible across Linix distros, etc
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CROP black from scanned images

Post by fmw42 »

You have not answered the questions that I asked above. We are trying to help. If we know your IM version and platform and your libjpeg version we can test with that version and see if there are any issue.

Try this simple command to make a 200x200 black image with a 100x100 white center.

Code: Select all

convert -size 100x100 xc:white -gravity center -background black -extent 200x200 test.jpg
Now try the -fuzz -trim on that

Code: Select all

convert test.jpg -fuzz 20% -trim +repage result.jpg
Do you get the original image as output or do you now get just a 100x100 white image?
Post Reply