Page 1 of 2

png transparent background is black

Posted: 2015-08-19T07:24:14-07:00
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.

Re: png transparent background is black

Posted: 2015-08-19T08:18:50-07:00
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

Re: png transparent background is black

Posted: 2015-08-20T08:57:54-07:00
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?

Re: png transparent background is black

Posted: 2015-08-20T10:41:54-07:00
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.

Re: png transparent background is black

Posted: 2015-08-20T10:59:33-07:00
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.

Re: png transparent background is black

Posted: 2015-08-20T11:12:42-07:00
by snibgo
Can you post the original png, not a jpg made from the png?

Re: png transparent background is black

Posted: 2015-08-20T12:33:16-07:00
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.

Re: png transparent background is black

Posted: 2015-08-20T12:34:24-07:00
by paphus
Here is one with the right extension,

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

Re: png transparent background is black

Posted: 2015-08-20T12:55:12-07:00
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

Re: png transparent background is black

Posted: 2015-08-21T11:07:53-07:00
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?

Re: png transparent background is black

Posted: 2015-08-21T11:25:17-07:00
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.

Re: png transparent background is black

Posted: 2015-08-21T12:13:29-07:00
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?

Re: png transparent background is black

Posted: 2015-08-24T06:12:03-07:00
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.

Re: png transparent background is black

Posted: 2015-08-25T09:41:32-07:00
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

Re: png transparent background is black

Posted: 2015-08-25T09:43:07-07:00
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.