ImageMagick: How to change transparent background to a color? - image-processing

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

Related

Set image inside png transparency

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.

Convert PNG with transparency to JPG

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.

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: animated gif background flatten

I'm wondering how to remove and replace transparency in an animated GIF with ImageMagick.
My goal is to take several pages with different aspect ratios and make an animated GIF that cycles through them.
document1.png
I am using
convert -delay 50 -resize 212 -dispose Previous -page 212x275 -crop 212x275+0+0 +repage document1.png document2.png documents.gif 2>&1
to create the GIF.
This sizes my documents correctly, but leaves the empty space from the short images transparent, but I would like that to be white. How can I change the transparency to white?
doc.gif
I have tried
convert doc.gif -background black -alpha off doc2.gif
convert doc.gif -background black -flatten doc2.gif
-alpha doesn't seem to do anything, and -flatten removes the animation

How to create transparent image for rubber stamp in ImageMagick

I have to create an image, which is an overlay texture made up of a white image with some transparency to give it the appearance of a rubber stamp.
For reference see the image "stamp_overlay.png" in the video http://railscasts.com/episodes/374-image-manipulation .
This is what i did:
convert -size 70x70 canvas:white stamp_overlay1.png
and then
convert stamp_overlay1.png -transparent white stamp_overlay1.png
But how do I make it like the image?
I am pretty much new to ImageMagick. Any help is highly solicited.
Updated Answer
Ok, I got that wrong! You want to create a stamp overlay, not overlay a stamp overlay. You can do that like this:
# Create white square, draw a black rectangle, then make black pixels transparent
convert -size 300x300 xc:white \
-fill black -draw "rectangle 20,100 200,280" \
-transparent black out.png
Original Answer
I find your question very hard to understand, but I think I know what you want. First, let's create a solid red image
convert -size 70x70 xc:red red.png
then let's composite the stamp_overlay.png image on top
convert red.png stamp_overlay.png -composite out.png
which gives this
but now you want to make the white areas transparent, so you need to do this:
convert red.png stamp_overlay.png -composite -transparent white out.png
and that still looks the same on this white background, but it isn't :-)

Resources