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.
Related
As the title suggests I have a PNG image that has some transparency. I'd like to fill that transparency with a second image (which is currently a JPEG, but it's not a problem to convert it to a PNG).
Every post I have found searching on the Internet was about the "inverse" problem (from an image with a background to an image with transparency), so obviously it did not work out for my situation; for example, I tried
convert -flatten myimg.png myimg.png
(taken from here) and
convert myimg1.png -transparent white myimg.png
(taken from here).
In ImageMagick 6, if the two images are the same size, then you can just flatten the transparent image over the background image.
Background (lena.jpg):
Transparent (logo_crop_trans.png):
convert lena.jpg logo_crop_trans.png -flatten lena_logo.jpg
If using ImageMagick 7, then change convert to magick.
If you want to anti-alias the transparent image so that it is not so jagged, then use some blur to smooth the outline (Unix syntax):
convert lena.jpg \( logo_crop_trans.png -channel a -blur 0x1 -level 50x100% +channel \) -compose over -composite lena_logo2.jpg
If on Windows remove the \ before the parentheses.
Given an input image sample.i.png with a transparent background :
How to convert its background into a file sample.o.png with background rgb(160,160,255), opacity 100% (or 1).
Answer used:
convert source.png -background "rgb(160,160,255)" -flatten out.png
Please +1 emcconville's answer !
From Mark Random's suggestion. Create a background image with your color, and flatten the source image over it.
convert -size 150x150 xc:"rgb(160,160,255)" source.png -layers flatten out.png
Edit
If you don't want to worry about image size, just change the background directly.
convert source.png -background "rgb(160,160,255)" -flatten out.png
Add the -flatten to re-set the background color
I want to add a tiled background to a transparent image.
According to the docs this should do it:
convert test.png -texture paper.png result.png
I also tried other variations (with -composite, -flatten, -tile, etc.) but result.png is either still transparent or just gets a white background.
ImageMagick-6.8.7-5 on Windows.
I got it to work with
convert -size 4096x4096 tile:paper.png test.png -flatten result.png
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
I'd like to use the ImageMagick convert tool to automatically generate a toolbar bitmap from several png images.
I'm using the following command line:
convert.exe -resize 32x32 #imagelist32.txt +append BMP3:toolbarlarge.bmp
with imagelist32.txt containing a list of png files (each one being one toolbar button).
This works, but the resulting bitmap uses black for the transparent color and white as the background color. I would need both colors to be RGB(192,192,192). Like if there was already an image with that background color, and the png images would be drawn on that background.
How can I do that? I've tried adding the -background #C0C0C0 and -transparent-color #C0C0C0 parameters but it didn't work - maybe I put them in the wrong order?
I know you've probably resolved it by your own, but I've been playing a bit with converter.exe some time ago, so I hope this is what you were looking for.
Set the -alpha parameter to the background flag, what means that every fully transparent pixel will be set to the background color, while leaving it fully transparent.
And set also the -background to a certain color RGB(192,192,192), so the previously transparent pixels will get this color.
convert.exe -resize 32x32 -alpha background -background RGB(192,192,192) #imagelist32.txt +append BMP3:toolbarlarge.bmp