ImageMagick - How do I flatten white levels to pure white? - imagemagick

I have a png image with a white background which I'd like to turn transparent. This is fairly simple with this command:
$ convert image.png -transparent white image-trans.png
However, if the white background is not completely white (i.e, #FFFFFF, rgb(255,255,255), etc), then this doesn't work well.
Is there a way to set reduce everything below a certain threshold to complete white? Thanks.

The commandline option you are looking for is
-white-threshold value{%}
So a command of
convert image.png \
-white-threshold 90% \
-transparent white \
image-trans.png
Note: Order of the respective parameters is significant! (You want first to convert all the light-gray pixels to white, then all white pixels to transparent.)
Works for me with 'ImageMagick version 6.7.8-0 2012-07-12 Q16'...

Related

How to remove specific white space from image using ImageMagick

Currently, I have thousands of pictures where I want to get rid of white space around the object, but not inside the object. However, the white space is not constant. See pictures below.
Picture 1 :
Picture 2 :
Picture 3 :
Options like this don't work as they delete all white space in the images
convert image.png -fuzz 10% -transparent white output.png
Any suggestions?
What do you mean by getting rid of the white space? If you mean make it transparent, then in ImageMagick 6, you can do a whole folder of images using the mogrify command as follows:
The process involves padding the image with 1 pixel border of white to ensure white is all around. Then doing a fuzzy flood fill. Then remove the extra one pixel all around. I have assumed that the background color is nearly white. Change the fuzz value as desired.
Create a new directory to hold the output (or backup your input directory)
Change directory to the one holding your input images
mogrify -path path_to/outdirectory -format png -bordercolor white -border 1 -fuzz 20% -fill none -draw "matte 0,0 floodfill" -shave 1x1 *.png
Here are the resulting images.
Of course you will have to download them to see that they are transparent.
Note that the images that I downloaded were JPG. So if your images are jpg and not png as in your command, then change *.png to *.jpg. Or just use * to change all formats.
See
https://imagemagick.org/Usage/basics/#mogrify
https://imagemagick.org/Usage/draw/#matte
If using ImageMagick 7, change matte to alpha and change mogrify to magick mogrify.

ImageMagick remove background noise and leave it white

In order to be better prepared for the OCR process, I want to remove the background noise on my images.
Right now I play with -lat parameter of ImageMagick tool:
magick.exe image_02_RESIZED.jpg -lat 20x20+10% test.jpg
but it completely removes the background from the image.
This is the sample output of the mentioned command:
Instead of this - I need to have the white background with a black text on it. How to do this with ImageMagick for Windows?
Try specifying the color you want to replace with -opaque and the new color with -fill.
magick image_02_RESIZED.jpg -fill white -opaque black test.jpg
Here is the result of this command:
You can also add a tolerance with -fuzz to remove noise around text; here is a sample command with tolerance set to 5%
magick image_02_RESIZED.jpg -fuzz 5% -fill white -opaque black test_05.jpg
This is the result of the command with -fuzz 5%:
You can experiment a bit to find the optimal value for -fuzz

How to create transparent icon using ImageMagick?

I'm trying to create icons (.ico format) with ImageMagick with only partial success. I can't get the resulting icon to have any transparency. I've tried many things like -alpha Background, -quantize transparent, -transparent-color, but I just can't get it working.
I can get it working if I don't reduce the colors -colors 256. It's with that reduction where I lose the transparency.
How do you produce a transparent icon with image magick (convert)?
I generate transparent PNGs with the -alpha transparent option. For example:
convert -size 100x100 -alpha transparent \
-stroke black -strokewidth 5 -fill blue \
-draw "rectangle 20,20,80,80" \
xc:#990000 test.png
This creates test.png with a floating blue box with a black border, inside an image with a transparent background. It uses a block colour as the "input" image , but the red is obliterated by the full transparency of its channel.
If you remove the alpha, you'll see the red source image.
Had the same problem when trying to generate a transparent favicon from transparent PNGs. Here's what worked for me:
convert transparent-img1.png transparent-img2.png transparent-img3.png -channel Alpha favicon.ico

Stop ImageMagick from pre-multiplying during composite

ImageMagick is premultiplying transparent pixels. This causes a gray outline to appear during subsequent transformations.
For example:
$ convert -size 1085x558 xc:"rgba(0,0,0,0)" PNG32:temp.png
$ composite -gravity center samples/logo_white.png temp.png PNG32:temp.png
Here are the source and resulting images.
Here is a video showing that the temp.png image has had its transparent pixels turned from white to black.
Is there a way to force ImageMagick to leave fully transparent pixels alone rather than changing them to black?

How to replace white background color with transparent of an image in ImageMagick?

I have an image in .jpg format with white background color. I want to remove the white background color to transparent in Imagemagick. I tried many ways but still the white background can not be removed. Can some one help me to solve this.
You cannot have transparent background colors in your JPEGs. The JPEG file format doesn't support transparency.
If you need transparent background, you need to convert the JPEG to
either PNG (high quality, filesize possibly larger than JPEG)
or GIF (in case you can tolerate low quality and a range of maximally 255 colors).
Example command:
convert your.jpg -transparent white your.png
First, you need to convert the image format from .jpg to .png format, because JPEG does not support transparency. Then use this command:
convert image1.png -fuzz 20% -transparent white result.png
The -fuzz option allows the specified percentage deviation from the pure white colour to be converted to transparent as well. This is useful, for example, when your image contains noise or subtle gradients.
I just found a very neat thing!
magicwand 1,1 -t 20 -f image -r outside -m overlay -o 0 image.jpg imgOutput.png
It is a Fred Weinhaus bash script that can be downloaded from here (for non commercial use only). Also there has about 250 scripts!! and this one is amazing! it did exactly the trick, to remove all background while keeping the inner image dots untouched!
At his page, there are several images as examples so you pick what you need to put on the command line!
The initial position 1,1 is a general guesser saying all the contour is background.
Pay attention that the output must be ".png"
This is my solution without magicwand (replace magick by convert for im < 7.0):
magick img.png -fuzz 20% -fill none -draw "alpha 1x1 floodfill" result.png
Get the background automatically and remove it :
bg=$(convert input.png -format "%[pixel:p{0,0}]" info:)
convert input.png -fuzz 20% -transparent "$bg" output.png

Resources