how to autocrop a deskewed scanned image - imagemagick

Following this approach, deskewing works great, how do I autocrop so it the outer border comes in until it finds a mostly white contiguous rectangle, so it could be auto-cropped after deskewing?

If you are using Imagemagick 7, you can do an extreme trim using the new -define trim:percent-background=0% to remove all the background from the image. See https://imagemagick.org/script/command-line-options.php#trim
Input:
magick skewed_1500.jpeg -background black -deskew 60% -background black -define trim:percent-background=0% -fuzz 1% -trim +repage x.jpg
Result:
ADDITION:
You can trim even more by trimming before the deskew. I had to use a large fuzz value to remove the bottom white. There must be some slight gray spot somewhere down there that I cannot see. But if there is no black border to start, you may trim too much white all around. That is the downside.
magick skewed_1500.jpeg -bordercolor white -border 1 -fuzz 75% -trim +repage -background black -deskew 60% -background black -define trim:percent-background=0% -fuzz 1% -trim +repage x.jpg
Result:

Related

Change Background in Image (Imagemagick, GraphicsMagick, ...)

I have scanned books with black imitation leather as background.
The text recognition unfortunately recognizes text on this background. I like to color the border black, so that the program does not find any text at the edge. Is this possible with tools like ImageMagick or GraphicsMagick?
Here is an example (original is in tif):
Perhaps a combination of floodfill and fuzz?
convert input.png -fill white -fuzz 20% -draw 'color 1,1 floodfill' output.png
Also checkout Fred's awesome textcleaner script.
emcconville has an excellent solution. I might add just a bit to it to include some deskew and trim/shave, since your margins are large enough to permit shaving the excess black that remains after a trim. The deskew might help in the OCR.
convert image.png -bordercolor black -border 1 -background black -deskew 40% -fuzz 50% -trim +repage -shave 10x10 result.png

Auto crop text(signature) from image and change background color using Imagemagick

Need to auto crop text(signature)from an images(sample image:Image1 )and Need to change background color of cropped image.
Need to achieve this using Imagemagick.
Is it any possible way to achieve these ?
I am using version ImageMagick 7.0.7-28
williamson image
example image
What do you mean by auto-crop? You can use ImageMagick -trim to get the signature cropped to its bounding box and then use -fuzz -opaque to change the background.
Input:
magick signature.png -fuzz 20% -trim +repage -fill pink -opaque white result.png
Or you can just make the background transparent.
magick signature.png -fuzz 20% -trim +repage -transparent white result2.png
I do not see any color change in the text, though it is not quite as smooth. Adjust the fuzz value as desired to remove the gray and keep the text as smooth as you can.
Input:
ImageMagick 7 command:
magick signature3.jpeg -fuzz 15% -fill white +opaque "#5B000C" -trim +repage result.png

combine two commands that swap white for black into one

I have a large number of images that are white with transparent backgrounds, and I would like to make them black with transparent backgrounds. It's simple enough to do with GIMP or BIMP, but with thousands of images, command-line seems a better way to go.
In ImageMagick, I've found that the following two commands do what I want:
mogrify -alpha set -channel RGBA -background black -flatten +repage -negate *.png
mogrify -alpha set -channel RGBA -transparent white *.png
However, I'd rather not make two passes. I've tried to combine them a number of different ways:
mogrify -alpha set -channel RGBA -background black -flatten +repage -negate +repage -transparent white *.png
mogrify -alpha set -channel RGBA -background black -flatten +repage -negate -alpha set -channel RGBA -transparent white *.png
mogrify -alpha set -channel RGBA -background black -flatten +repage -negate +repage -alpha set -channel RGBA -transparent white *.png
as well as a couple other permutations of the same ideas. All of them result in a purely black image. What am I missing?
Is there an easier way to invert black and white or at least convert white to black, but leaving the alpha layer untouched?
mogrify -negate *.png
converts white to transparent and transparent to white, and
mogrify -fill black -opaque white *.png
leaves behind messy white edges.
In ImageMagick you can do:
mogrify -format png -fill "rgba(0,0,0,1)" -opaque "rgba(255,255,255,1)" *.png
If your white is not perfectly white, then add -fuzz XX%
mogrify -format png -fuzz 5% -fill "rgba(0,0,0,1)" -opaque "rgba(255,255,255,1)" *.png
The issue is that you need to specify alpha values in your colors, since your image has transparency. Thus use rgba(r,g,b,a) values (note the a) or use hex values with #RRGGBBAA
If using IM 7, then mogrify is replace with magick mogrify
Here is an example using convert. I created a white image with transparency elsewhere from the logo: image.
http://www.fmwconcepts.com/misc_tests/transparency_invert/logot.png
Then ran
convert logot.png -fill "rgba(0,0,0,1)" -opaque "rgba(255,255,255,1)" logot_invert.png
Which returns black where the input was white and keeps the transparency unchanged.
http://www.fmwconcepts.com/misc_tests/transparency_invert/logot_invert.png
Is this not what you want?
Perhaps what you want is the following in ImageMagick. Just a guess until I can see your actual input files.
mogrify -format png -alpha off -negate -alpha on *.png
In ImageMagick, try
mogrify -alpha off -negate -alpha on *.png
This turns off the alpha channel inverts the black and white and then turns on the original alpha channel.
As a test, I did
convert aircon.png -alpha off -negate -alpha on aircon_fred.png
The equivalent of your two mogrify command is
mogrify -background black -alpha background -alpha off -negate -transparent white *.png
I had to replace your -flatten with the equivalent since one needs to reset the compose method to over afterwards for the -transparent to work. But mogrify does not accept -compose over.
As a simple test, I did
convert aircon.png -background black -flatten -negate -transparent white aircon_fred2.png
Another method similar to the first is just to make the whole underlying image black and keep the alpha channel.
mogrify -alpha off -fill black -opaque white -alpha on *.png
Again as a test, I did
convert aircon.png -alpha off -fill black -opaque white -alpha on aircon_fred3.png
However, my first method should give better antialiasing, since it keeps your original alpha channel. Your method will recreate the alpha via -transparent white and will have more stair-stepped aliasing.

Convert the background of an image to transparent

I want to convert the background of a image to transparent. I have used the below script for converting. Also i am converting the image to png.
convert -resample 300x300 -depth 8 "%1"[0] -fuzz 10% -transparent white -flatten -resize 1260x1260 -quality 80 "%2".
I also tried below command:
convert -resample 300x300 -depth 8 "%1"[0] -background none -flatten -resize 1260x1260 -quality 80 "%2"
but using the above script background is not converted to transparent.
Can you please let me know the Imagemagick script for converting the background.
Thanks in advance.
Updated Answer
You really need to show your image, but if the background his white as you say, this should work:
convert start.png -fuzz 40% -transparent white result.png
If that doesn't work, I really cannot help till you show the image.
Original Answer
You need to be clearer as to what colour the background currently is, and how it can be identified - or show your image.
If we assume this is your starting image:
then we can make the blue pixels transparent like this:
convert start.png -fuzz 10% -transparent blue result.png
If we take this as your start image:
and set the fuzz to "within 10% of black", we will do this:
convert start.png -fuzz 10% -transparent black result.png
whereas if we make pixels within 30% of black become transparent:
convert start.png -fuzz 30% -transparent black result.png
we will get this:

ImageMagick: How to make both black and white transparent?

Converting black and dark shades to transparent works fine with ImageMagick.
I even managed to perform a crop and resize in the same line.
convert input.png -background none -fuzz 45% -transparent black -flatten -crop 640x480+12+9 -resize 105% output.png
However, the input image also contains a number of almost white lines, which I also would like to convert to transparent in the output.
How do I go about that? Is it possible to do it within the same command line?
Sure, just add a second -transparent.
convert -size 512x512 gradient:black-white a.png # create initial black-to-white gradient
convert -fuzz 20% a.png -transparent black -transparent white result.png # lose 20% off black end and white end
Or, with extra fuzz...
convert -fuzz 40% a.png -transparent black -transparent white result.png

Resources