Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

transparency - ffmpeg convert white color to alpha

Hello i took a picture with my camera :a drawing of a square frame on a white paper; i'm trying to convert the white to transparent and keep the black color frame.

So i started making 1 palette with only 2 colors to make the colors uniform and 1 palette to add the transparency

ffmpeg -f lavfi -i "color=color=white:100x100" -f lavfi -i "color=color=black:100x100" -filter_complex "[0][1]hstack" -frames:v 1 blackwhite.png`
ffmpeg -i blackwhite.png -filter_complex "[0]split[a][b];[a]palettegen[pal1];[b]palettegen=reserve_transparent=on:transparency_color=white[pal2]" -map [pal1] palette1.png -map [pal2] palette2.png

then i mapped the image png of the frame to convert the white to transparent and overlayed the result to a red background

ffmpeg -i image.png -i palette1.png -i palette2.png -i background.png -filter_complex "[0:v][1:v]paletteuse=dither=bayer[a],[a]split[a1][a2];[a1][2:v]paletteuse=alpha_threshold=128[c];[3:v][c]overlay[d]" -map [a2] -c:v png out.png -map [d] -c:v png out1.png

the png mapped to the first palette (as a test) comes as it should be, pure black and white,the second comes with no transparency at all and covers the background what am i doing wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ok i figured out a way with the alpha-merge filter, there's no need for the second palette that allows transparent.

so i started simply

ffmpeg -i image.png -i palette.png -filter_complex "paletteuse=dither=bayer" image2colors.png
ffmpeg -i background.png -i image2colors.png -filter_complex "alphamerge" out.png

that convert the entire image to an alpha channel(both black and white are transparent)

ffmpeg -i image2colors.png -i background.png -filter_complex "[0]split[a][b];[a][b]alphamerge[c];[1][c]overlay[d]" -map [d] out.jpeg

this keeps the color white and made the black of the drawing transparent that, when overlayed, becomes red, also the color white is not so bright anymore, like it has a sort of transparency

this solved the question

ffmpeg -i image2colors.png -i background.png -filter_complex "[0]split[a][b];[a]negate[n];[b][n]alphamerge[c];[1][c]overlay[d]" -map [d] out.jpeg

that gave me a perfect black frame on alpha-channel overlayed to a colored background.(the jpeg shows that the overlay has gone correctly) Seems that the alpha-merge chooses the black over the white. i'm posting all of my mistakes because i made it by attempts and i still don't know what i'm doing. if anyone has a more elegant way to obtain this result please answer to me with a lot of details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...