【问题标题】:GIF Frames IterationGIF 帧迭代
【发布时间】:2020-05-26 09:47:24
【问题描述】:

我正在尝试提取 gif 文件的所有帧以列出 与 PIL,以执行它们的其他操作。

from PIL import Image

im = Image.open(r'C:\Users\Jakub\Documents\test.gif')

frames = []
for x in range(0, im.n_frames):
    im.seek(x)  # `im.seek(im.tell()+1)` returns None
    frames.append(im)

frames # returns [Image1, Image1, Image1, ...]

我怎样才能达到这个结果:

...
>>> frames
[Image1, Image2, Image3, ...]

【问题讨论】:

    标签: python loops python-imaging-library gif frames


    【解决方案1】:

    你可能想试试my GIF library:

    from PIL import Image
    
    def GIF_Load(file):
        from platform import system
        from ctypes import string_at, Structure, c_long as cl, c_ubyte, \
                           py_object, pointer, POINTER as PT, CFUNCTYPE, CDLL
        class GIF_WHDR(Structure): _fields_ = \
           [("xdim", cl), ("ydim", cl), ("clrs", cl), ("bkgd", cl),
            ("tran", cl), ("intr", cl), ("mode", cl), ("frxd", cl), ("fryd", cl),
            ("frxo", cl), ("fryo", cl), ("time", cl), ("ifrm", cl), ("nfrm", cl),
            ("bptr", PT(c_ubyte)), ("cpal", PT(c_ubyte))]
        def intr(y, x, w, base, tran): base.paste(tran.crop((0, y, x, y + 1)), w)
        def skew(i, r): return r >> ((7 - (i & 2)) >> (1 + (i & 1)))
        def fill(w, d, p):
            retn = Image.new("L", d, w.bkgd) if (w.tran < 0) else \
                   Image.new("RGBA", d)
            if (w.tran < 0):
                retn.putpalette(p)
            return retn
        def WriteFunc(d, w):
            cpal = string_at(w[0].cpal, w[0].clrs * 3)
            list = d.contents.value[0]
            if (len(list) == 0):
                list.append(Image.new("RGBA", (w[0].xdim, w[0].ydim)))
            tail = len(list) - 1
            base = Image.frombytes("L", (w[0].frxd, w[0].fryd),
                                   string_at(w[0].bptr, w[0].frxd * w[0].fryd))
            if (w[0].intr != 0):
                tran = base.copy()
                [intr(skew(y, y) + (skew(y, w[0].fryd - 1) + 1, 0)[(y & 7) == 0],
                      w[0].frxd, (0, y), base, tran) for y in range(w[0].fryd)]
            tran = Image.eval(base, lambda indx: (255, 0)[indx == w[0].tran])
            base.putpalette(cpal)
            list[tail].paste(base, (w[0].frxo, w[0].fryo), tran)
            list[tail].info = {"delay" : w[0].time}
            if (w[0].ifrm != (w[0].nfrm - 1)):
                trgt = (tail, d.contents.value[1])[w[0].mode == 3]
                list.append(list[trgt].copy() if (trgt >= 0) else
                            fill(w[0], (w[0].xdim, w[0].ydim), cpal))
                if (w[0].mode != 3):
                    d.contents.value[1] = w[0].ifrm
                if (w[0].mode == 2):
                    list[tail + 1].paste(fill(w[0], (w[0].frxd, w[0].fryd), cpal),
                                                    (w[0].frxo, w[0].fryo))
        try: file = open(file, "rb")
        except IOError: return []
        file.seek(0, 2)
        size = file.tell()
        file.seek(0, 0)
        list = [[], -1]
        CDLL(("%s.so", "%s.dll")[system() == "Windows"] % "./gif_load"). \
        GIF_Load(file.read(), size,
                 CFUNCTYPE(None, PT(py_object), PT(GIF_WHDR))(WriteFunc),
                 None, pointer(py_object(list)), 0)
        file.close()
        return list[0]
    

    【讨论】:

      猜你喜欢
      • 2018-03-12
      • 1970-01-01
      • 2018-07-10
      • 2019-04-23
      • 2020-10-11
      • 1970-01-01
      • 2023-04-03
      • 2014-12-22
      • 1970-01-01
      相关资源
      最近更新 更多