How can I add a Background Tile to a transparent Image? - imagemagick

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

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.

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 :-)

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

create toolbar imagelist with imagemagick

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

Resources