如何确定动画 GIF 帧中透明像素的数量?

How to determine the number of transparent pixels in an animated GIF frame?

我知道动画 GIF 帧中的透明像素由透明颜色索引确定,当透明颜色标志为设置为 1。

但是,我不知道如何判断动画GIF帧的透明色索引是多少,是否设置了透明色标志,以及如何判断帧中哪些像素是这种颜色。我查看了诸如 ImageMagick, wand, and Pillow 之类的工具,但我无法制定解决方案来查找透明像素的数量。

一些伪代码(Python 风格)我想制定我的解决方案:

def count_transparent(frame):
    count = 0
    if transparent_flag(frame):
        transparent_pixel = find_transparent_pixel(frame)
        for row in frame:
            for col in frame:
                # Compare the colors
                if frame[row][col] == transparent_pixel:
                    count += 1
    return count

问题

我可以使用什么tools/methods(以及如何使用它们)来获取信息以查找与每个动画 GIF 帧的透明颜色标志和透明像素相关的信息,我也可以在其中使用遍历帧的每个位置?

我找到的最接近的是 gifsicle,它似乎允许我找到有关透明颜色标志和透明像素的信息,但我无法用它遍历帧的每个位置。我很欣赏可以与 Python 一起使用的东西,但也欢迎任何语言的解决方案。

在 ImageMagick 中,您可以通过提取 alpha 通道来确定透明像素的数量,将其取反以使透明为白色,然后获取该图像的平均值(在 0 到 1 范围内)乘以宽度*高度。对于第 10 帧(从 0 开始)

convert image.gif[9] -alpha extract -negate -format "%[fx:mean*w*h]" info:

应该给你透明像素的数量。