png transparent background is black

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

png transparent background is black

Post by paphus »

I'm trying to convert a png image series with a transparent background to an mpeg and webm video.
The transparent background shows as black in the video but I want it to be white.

I know I can convert the background to white using,

mogrify -flatten *.png

But I want to preserve the transparency for video players that support it (Chrome) but be white for those that do not (Firefox).

I have some png files that work, and are white, but other come in as black, so their must be something in the png image making the background black.

I tried,

mogrify -transparent-color white -opaque white -fill white -background white *.png

But it was still black.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: png transparent background is black

Post by snibgo »

In IM, "transparent" often means the pixel is black, but transparent. In your situation perhaps you want transparent pixels to be white-transparent.

Code: Select all

-background White -alpha Background
See http://www.imagemagick.org/script/comma ... .php#alpha
snibgo's IM pages: im.snibgo.com
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

Re: png transparent background is black

Post by paphus »

Thanks, that works, but I get a weird halo around the image. My guess is there are semi transparent pixls.

With,

> mogrify -background white *.png

> mogrify -alpha Background *.png

I get this webm, which looks good in Chrome, but has a halo in Firefox

http://www.botlibre.com/media/a1153572.webm

same with the mp4

http://www.botlibre.com/media/a1153571.mp4

If I use flatten, the mp4 looks like,

> mogrify -flatten *.png

http://www.botlibre.com/media/a1153573.mp4

But need solution for transparent webm on Firefox still.

I think I'm close, but need to somehow use flatten but preserve the alpha channel.

Any ideas, I'm sure mogrify can do this, I just don't know how?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: png transparent background is black

Post by snibgo »

I don't which of those is your source, if any. Are they all your outputs? You need to carefully examine a frame from the source. I suppose the source frames have the figure against a black background, so pixels at the edge of the figure are a mixture of the pink flesh etc and black. When you turn pixel that are exactly black to some other colour, the dark "halo" remains. The trick is probably to erode the figure, to remove the dark halo.
snibgo's IM pages: im.snibgo.com
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

Re: png transparent background is black

Post by paphus »

The source images are a png image series, all like,

http://www.botlibre.com/avatars/a1153644.jpg

The issue is the background color used to render the image was black but with a transparent alpha.
Flatten overlays a white background with the png image with transparency, so the black becomes white, which is what I want, and it looks good.
But I still need the alpha transparency, so I need some way to merge just the alpha channel from the original image with the flattened image.
I think that will give me what I want, but I don't know the command the merge the alpha from one png image into another.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: png transparent background is black

Post by snibgo »

Can you post the original png, not a jpg made from the png?
snibgo's IM pages: im.snibgo.com
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

Re: png transparent background is black

Post by paphus »

Oops, actually the file is a png, the file extension is wrong. You can save it using .png it does have the alpha transparency.
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

Re: png transparent background is black

Post by paphus »

Here is one with the right extension,

http://www.botlibre.com/media/a1153704.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: png transparent background is black

Post by snibgo »

In the PNG file, the figure is surrounded by halos of various colours at low alpha, with a general background of black-transparent. If you merely flatten or do anything to remove alpha, you can see these halos:

Code: Select all

convert a1153704.png  -alpha off a2.png
If we threshold the alpha at 5%, any alphas lower than this will become zero. Then we can make fully transparent pixels into transparent black, or transparent white, or whatever we want.

Code: Select all

convert a1153704.png -channel A -threshold 5% +channel -background Black -alpha background a2.png
snibgo's IM pages: im.snibgo.com
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

Re: png transparent background is black

Post by paphus »

Thanks but it makes it worse, it looses part of the edges.

http://www.botlibre.com/media/a1156752.webm

http://www.botlibre.com/media/a1156753.png

I tried 1% as well, then it looses some image, and gets a halo...

The flatten image and video looks good, it just is missing the alpha. Is there any way to add the alpha channel back into the flattened image?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: png transparent background is black

Post by snibgo »

There are weird pixels in the hair at alpha=27%. You might extend the alpha threshold to include them. "-alpha extract" and "-compose CopyOpacity -composite" will copy alpha to a new image, and copy it back.
snibgo's IM pages: im.snibgo.com
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

Re: png transparent background is black

Post by paphus »

Great, I got it to work, but still need to be able to do it simpler,

convert -alpha extract *.png mask.png
mogrify -flatten blink*.png
composite -compose CopyOpacity mask-0.png blink00.png good.png

Mask the final video good,
http://www.botlibre.com/media/a1159578.webm

but there are 60 files, blink00.png -> blink60.png, how do I do the last step to merge all of the files?
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

Re: png transparent background is black

Post by paphus »

Does anyone know how to batch convert 60 files with the above three steps? (extract mask, flatten, compose mask with flattened image)
Just the last step I can't figure out.
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

Re: png transparent background is black

Post by paphus »

Ok what I came up with was using a shell for command,

for /f %x in ('dir /s /b blink*.png') do @composite -compose CopyOpacity mask-0.png %x %x

But for some reason when I convert from the png series to a webm video using ffmpeg now it does not include the alpha channel. It does from the original files, and I do see the alpha in the new files. So I'm at my wits end on this one.

I'm using,

ffmpeg -framerate 10 -i blink%02d.png -r 10 blink.webm
paphus
Posts: 13
Joined: 2015-08-19T07:18:29-07:00
Authentication code: 1151

Re: png transparent background is black

Post by paphus »

Not sure how it worked the first time. But I was trying a lot of things, maybe somehow used another step the first time to get a good video. Please help.
Post Reply