hackerb9 / mktrans

Make background transparent using antialiased alpha channel
MIT License
65 stars 12 forks source link

question if it works for gifs? #4

Open JKP1008shine opened 6 months ago

JKP1008shine commented 6 months ago

I want to remove the background of my gifs with this script, any additional configurations have to be made or the usage is same for gifs as mentioned in the readme?

hackerb9 commented 6 months ago

Good question. I don't think I tested it with animation. My guess is it won't work and you'd have to explode the GIF to individual files. [Update: I have tested it and it definitely does not work.]

One reason I didn't test it with animated images is because the GIF format (from 1989) has only a 1-bit alpha channel, which looks grotty because each pixel is either transparent or opaque with no in-between. For such images, often just picking a single color to turn transparent works better and is super fast and easy. (convert input.gif -fuzz 50% -transparent blue output.gif). My script outputs PNG format because it has 8-bit alpha (256 levels of transparency), but PNG can only show static images, not animation.

However, nowadays people often call any animated video without sound a "GIF" and modern formats (WEBP, WEBM) support the same color depth as PNG (24-bit color, 8-bit alpha) and can play animations. So, maybe I should consider outputting to a lossless WEBP instead of PNG so that animated files have a chance to work.

On the other hand... this is a very simple script and I don't want to complicate it with features that perhaps people don't even want. And, there's the question of whether the simplifying presumptions that work well-enough for a static image hold for the kinds of animations that people want. Is the "fuzz" factor going to be the same on each frame? Does choosing the top-left pixel as the background work for all frames?

If it starts getting complicated, I think people may want to just go for an industrial strength solution, like ffmpeg:

ffmpeg -i input.mp4 -vf "colorkey=0x000000:0.06:0.06" -c:v libwebp_anim -lossless 0 -compression_level 6 -q:v 50 -loop 0 -an -preset picture -metadata:s:v:0 alpha_mode="1" output.webp