Say I have a PNG with transparent pixels, how could I translate them to a solid color?
(Note this is slightly different to this question as I'm looking to replace with an arbitrary color. White is "special" in ImageMagick, as the accepted answer there indicates by the fact it omits to mention white).
For me, the following works (ImageMagick 6.6.3-1):
convert in.png -background "#FF0000" -flatten out.png
Just swap the red color to whatever suits your needs.
Related
I have question. Is it possible to delete all colors from image, but save the black color? I have a picture with several unknown colors. (Therefore I can't just replace e.g red color with white color). I have an image like this:
And I am trying to delete this "Text2" and "Text3". Is that possible? Which option in Imagemagick should I use?
Mark's answer is probably the best if you easily can separate the black region from the rest of the image, since it preserves the antialiased text better. However, if not then you can do something similar to Bonzo's command. Here is another variation of that.
convert EWwSX.jpg -fuzz 40% -fill white +opaque black result.png
Not sure I understand your question - you can't really delete a colour in an image. What would be left? I understand you can't replace the red with white because you have reds elsewhere in your image.
I guess the easiest thing to do is draw a white rectangle over the unwanted text:
convert text.jpg -fill white -draw "rectangle 20,72 100,150" result.jpg
Not a great result but with some tweeking or work you could improve it:
convert EWwSX.jpg -threshold 20% black.png
Edit: The original title of this question was "Drawbacks of using -flatten option". However, after this question was answered, I decided to change its title, to make it easier found on Google. Also, new title is actually better describes what is told below.
As I discovered today, to convert PNG with transparency into JPG, we need to use -flatten option.
Try it yourself: download Google logo and convert it from PNG to JPG with the following line
convert google.png google.jpg
the picture will be messed:
With -flatten option, it works fine:
convert google.png -flatten google.jpg
I'm wondering are there any drawbacks of using -flatten permanently, for all conversions between PNG, JPG/JPEG and GIF.
The problem with converting PNG to JPG is when the PNG has transparency. JPG does not allow transparency and any transparent areas will show what color is underneath the transparency, which is often black. So you should use -flatten to properly do that conversion. But you should specify -background somecolor before -flatten, if you do not want the default background color. GIF only allows binary transparency -- fully transparent or fully opaque. PNG allows 8-bit transparency (partial transparent). I know of no significant issues using -background xx -flatten when converting PNG or GIF to JPG. However, the background color you use will change the appearance in transparent areas from that of the underneath color. Here is what is happening:
Input:
Turn alpha off:
convert google.png -alpha off google_aoff.jpg
The stripes are from the underneath color below the alpha channel.
Alpha Channel (nicely antialiased):
convert google.png -alpha extract google_alpha.jpg
Simple Flatten (default background is white):
convert google.png -flatten google_flatten.jpg
Flatten with black background:
convert google.png -background black -flatten google_flatten_black.jpg
Often one will reprocess the original transparent PNG image so that it has some constant color underneath the alpha channel so that later one can remove the alpha channel and not have odd colors showing. It will look the very same as the original PNG.
convert google.png -background white -alpha background google_bg_white.png
However, if you simply remove the alpha channel the JPG will show aliasing since only the fully transparent pixels' background colors were changed to white. You have a nice clean background, but the image is still aliased (as it was in the original when the alpha channel was remove).
convert google_bg_white.png google_bg_white.jpg
So one still needs to flatten the result, so that the antialiasing of the alpha channel will smoothly blend the colors near the boundaries.
convert google_bg_white.png -flatten google_bg_white_flatten.jpg
An alternate method to -flatten is to use -alpha remove, which is discussed http://www.imagemagick.org/Usage/masking/#alpha_remove. So starting with the original PNG, we do
convert google.png -background white -alpha remove google_alpharemoveoff.jpg
The result is the same as -background white -flatten. We do not need the -alpha off mentioned in the reference, since JPG does not support any alpha channel. The reference says this is more efficient and is the preferred method.
#John C wrote:
1st approach:
convert google.png -flatten google_flatten.jpg
2nd approach:
convert google.png -background white -alpha background google_bg_white.png
convert google_bg_white.png -flatten google_bg_white_flatten.jpg
3rd approach:
convert google.png -background white -alpha remove google_alpharemoveoff.jpg
More properly, these should be
1st approach
convert google.png -background white -flatten google_flatten.jpg
2nd approach
convert google.png -background white -alpha background -flatten google_bg_white_flatten.jpg
3rd approach
convert google.png -background white -alpha remove -alpha off google_alpharemoveoff.jpg
In case 1: -background white is the default. But if you want some other background color you need to specify it.
In case 2: there is no need to save to an intermediate file
In case 3: you will need -alpha off if you save to PNG. JPG does not support transparency, so turning alpha off is not needed.
Let's say I have a PNG image with some random colors in it, like this one:
The goal is to make every color in the image EXCEPT a specific color (like #FCFF00) with pure black (#000000). So for example, if I gave the code the above image, it would replace it with this:
How would I do this?
This would be the +opaque option.
convert YiCYA.png -fill black +opaque '#FC00FF' output.png
I an trying to use Image Magick to create a new large png from several small png's but the smaller image do not have their transparency preserved. I am creating a 6000x6000 image and placing smaller png's at specific locations and some of them being rotated, this all works fine. The problem is that the small images don't have their transparency preserved when some of the small image overlap. This is an example of what I mean. I have tried several -channel options and -alpha on but nothing seems to work. What am i missing here?
Here is the commands I am using for my test.
convert -size 6000x6000 xc:none ^
( Rectangle_01.png -repage +200+200 ) ^
( Rectangle_01.png -repage +651+200 -rotate 45 ) ^
( Rectangle_01.png -repage +1102+200 -rotate -45 ) -flatten -alpha on test.png
Did you try:
-background none
none is one of the built-in color names for a fully transparent color.
I found this question when trying to solve a similar problem with the montage command, that would not preserve transparency. Setting a transparent background did fix my problem. The default background in ImageMagick is white.
There is a similar topic on http://www.imagemagick.org
"Try:
-fuzz XX% -transparent white
where the smaller the %, the closer to true white or conversely, the larger the %, the more variation from white is allowed to become transparent."
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=12619
Suppose a dir with a lot of images, some of them miss the alpha-channel so looking bad against a background. I thought every picture had alpha -channel but some pictures now show white instead of having transparency. How can I add the alpha channels en masse?
Not Working
$ convert imageNoAlpha.png -alpha on imageAlpha.png
This will change white to transparent as in you coment BUT any other white in the image will be transparent as well:
convert input.jpg -background none -transparent white -flatten output.png
Thanks to Bonzo, I was able to get very cool results even without anti-alias! You can see the difference in the eyes with the below picture. I don't know how much you lose information by this but at least I am more able to adjust things, not so easy in anti-aliasing at least for me.
$ convert in.png -background none -fuzz 10% -transparent white -flatten out.png
En masse -approach is probably easiest with a bash -for-loop or use ImageMagick -pkg built for python. I don't know which approach is better, I will leave this here for future answers to improve. Thanks.