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

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

Related

how to autocrop a deskewed scanned image

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:

How to add a border to a transparent PNG using ImageMagick, while preserving transparency?

I am trying to add a 10px red border to a transparent PNG using ImageMagick, while preserving any existing transparency that might exist within the image. Here is my source image:
If you download and view that image with an image viewer, you'll see that it has a transparent background.
According to everything I've read, the following Imagemagick command should add a 10px red border to the image:
convert input.png -bordercolor red -border 10 output.png
It actually does add the red border to the image, since the output dimensions are 20px larger in both directions. Unfortunately it also changes the background color of the image to red as well. Here is the output file:
I do not want the transparent area to be changed to red. I only want to add a red border around the transparent image.
I've tried using both ImageMagick version 6.9.10-23 (Ubuntu) and 7.1.0 (via CloudConvert API), with the same result. I've spent hours(!) trying to solve this.
What am I doing wrong?
I found the answer in this thread: https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=31843 . Here are the two money quotes:
So, "-bordercolor red -border 2" should create an opaque red image 2
pixels larger than the input, and composite the input over this. As
your input is "-size 100x100 xc:none", the result should be 102x102
opaque red pixels. You might think this is "pretty obviously
incorrect", but it is the documented behaviour.
and
Nevertheless, you can get it to work to have the transparent inside,
if you add -compose copy before -bordercolor red -border 2 in both the
current IM 6 and IM 7. This just may have to be the way to do it from
here on, if there is a good reason for the changed behavior.
Here is the command that produces the result I am after:
convert -background transparent -bordercolor red -compose Copy -border 10 input.png output.png
Here's an answer that fully preserves the transparency
convert input.png +write mpr:INP -alpha extract -morphology dilate disk:10 \\( +clone -fill Black -colorize 100 \\) +swap -compose CopyOpacity -composite mpr:INP -compose Over -composite output.png
From https://legacy.imagemagick.org/Usage/crop/#extent
Here is a simple way to do that in Imagemagick. Change the compose setting from over to copy.
Input:
convert logo_transp.png -compose copy -bordercolor red -border 10 logo_transp_border.png

remove white background with ImageMagick but not the white inside picture?

im trying to delete the background with a batch process in pictures of products for an e-commerce site.
The problem is that the script also remove the white color in the inside of the product, leaving the product transparent in some areas..
For example:
Command:
convert *.jpg -set filename: %t -fuzz 5% -transparent white %[filename:].png
this is the best I can get..im ok with this result around the product, but I need that the white inside the product remains white and not transparent.
The problem with your current approach is that it doesn't respect boundaries, it is just applied globally, making all white pixels transparent regardless of their connectivity to the background.
Instead, you will get on better using a "floodfill" that only floods into areas that are within the fuzz distance of the top-left corner pixel.
So, I chose an unused colour of magenta so you can see what is happening:
convert product.jpg -fuzz 5% -fill magenta -draw 'color 0,0 floodfill' result.png
You would then follow that with the command to make magenta transparent like this:
convert product.jpg -fuzz 5% -fill magenta -draw 'color 0,0 floodfill' -transparent magenta 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

Set any white pixels to transparent in ImageMagick using a threshold?

Using ImageMagick, I want to find any pixels that are white and make them transparent. I'm thinking the key is -threshold but I can't work out how to change a white pixel to transparent using -threshold. Any suggestions most appreciated.
convert input.png -fuzz 10% -transparent white output.png
Note that the order of the arguments is important - -fuzz must come before -transparent.
Would something like this work?
convert input.jpg -fuzz 5% -fill to_color -opaque from_color output.jpg

Resources