Warm tip: This article is reproduced from serverfault.com, please click

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

(python - How to determine the number of transparent pixels in an animated GIF frame?)

发布于 2020-12-05 14:51:47

我知道动画GIF帧中的透明像素由透明颜色索引确定,当透明颜色标志设置为1时,我们可以通过将像素分配给透明颜色索引的值来将像素设置为透明。

但是,我不知道如何确定动画GIF帧的透明颜色索引是什么,是否设置了透明颜色标志,以及如何确定帧中的哪些像素是这种颜色。我环顾了诸如ImageMagickwandPillow之类的工具,但无法制定解决方案以找到透明像素的数量。

我想到了一些伪代码(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

问题

我可以使用哪些工具/方法(以及如何使用它们)来获取信息,以查找与每个动画GIF帧的透明色标志和透明像素有关的信息,还可以在其中迭代帧的每个位置?

我找到的最接近的是gifsicle,它看起来使我能够找到有关透明色标志和透明像素的信息,但是我无法使用它遍历框架的每个位置。我很高兴可以在Python中使用某些东西,尽管也欢迎使用任何语言的解决方案。

Questioner
Theger14
Viewed
0
fmw42 2020-12-06 03:01:17

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

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

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