当前位置: 首页>>代码示例>>Python>>正文


Python PIL.GifImagePlugin类代码示例

本文整理汇总了Python中PIL.GifImagePlugin的典型用法代码示例。如果您正苦于以下问题:Python GifImagePlugin类的具体用法?Python GifImagePlugin怎么用?Python GifImagePlugin使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了GifImagePlugin类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_getdata

    def test_getdata(self):
        # test getheader/getdata against legacy values
        # Create a 'P' image with holes in the palette
        im = Image._wedge().resize((16, 16))
        im.putpalette(ImagePalette.ImagePalette('RGB'))
        im.info = {'background': 0}

        passed_palette = bytes(bytearray([255-i//3 for i in range(768)]))

        GifImagePlugin._FORCE_OPTIMIZE = True
        try:
            h = GifImagePlugin.getheader(im, passed_palette)
            d = GifImagePlugin.getdata(im)

            import pickle
            # Enable to get target values on pre-refactor version
            # with open('Tests/images/gif_header_data.pkl', 'wb') as f:
            #    pickle.dump((h, d), f, 1)
            with open('Tests/images/gif_header_data.pkl', 'rb') as f:
                (h_target, d_target) = pickle.load(f)

            self.assertEqual(h, h_target)
            self.assertEqual(d, d_target)
        finally:
            GifImagePlugin._FORCE_OPTIMIZE = False
开发者ID:anntzer,项目名称:Pillow,代码行数:25,代码来源:test_file_gif.py

示例2: test_number_of_loops

    def test_number_of_loops(self):
        number_of_loops = 2

        out = self.tempfile('temp.gif')
        with open(out, "wb") as fp:
            im = Image.new('L', (100, 100), '#000')
            for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, loop=number_of_loops):
                fp.write(s)
            fp.write(b";")
        reread = Image.open(out)

        self.assertEqual(reread.info['loop'], number_of_loops)
开发者ID:Paxxi,项目名称:Pillow,代码行数:12,代码来源:test_file_gif.py

示例3: test_duration

    def test_duration(self):
        duration = 1000

        out = self.tempfile('temp.gif')
        with open(out, "wb") as fp:
            im = Image.new('L', (100, 100), '#000')
            for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, duration=duration):
                fp.write(s)
            fp.write(b";")
        reread = Image.open(out)

        self.assertEqual(reread.info['duration'], duration)
开发者ID:Paxxi,项目名称:Pillow,代码行数:12,代码来源:test_file_gif.py

示例4: test_number_of_loops

    def test_number_of_loops(self):
        number_of_loops = 2

        out = self.tempfile("temp.gif")
        fp = open(out, "wb")
        im = Image.new("L", (100, 100), "#000")
        for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, loop=number_of_loops):
            fp.write(s)
        fp.write(b";")
        fp.close()
        reread = Image.open(out)

        self.assertEqual(reread.info["loop"], number_of_loops)
开发者ID:njones11,项目名称:Pillow,代码行数:13,代码来源:test_file_gif.py

示例5: test_duration

    def test_duration(self):
        duration = 1000

        out = self.tempfile("temp.gif")
        fp = open(out, "wb")
        im = Image.new("L", (100, 100), "#000")
        for s in GifImagePlugin.getheader(im)[0] + GifImagePlugin.getdata(im, duration=duration):
            fp.write(s)
        fp.write(b";")
        fp.close()
        reread = Image.open(out)

        self.assertEqual(reread.info["duration"], duration)
开发者ID:njones11,项目名称:Pillow,代码行数:13,代码来源:test_file_gif.py

示例6: __iadd__

 def __iadd__(self, other):
     if self.previous is None:
         # Globaler Header
         header = "".join(GifImagePlugin.getheader(other))
         header = header.replace("GIF87a", "GIF89a")
         self.fo.write(header)
         # Graphic Control Extension-Block
         header = [
             "\x21",  # Signatur für Beginn eines
             # Extension-Blocks
             "\xf9",  # Graphic Control Extension-Signatur
             "\x04\x00",  # Größe des Blocks (4 Bytes)
             "\x0a\x00",  # Irgendwas + reservierter Block
             "\xff\x00",  # Pause zwischen Frames (1/100 s)
         ]
         self.fo.write("".join(header))
         # Application Extension Block
         header = [
             "\x21",  # Signatur für Beginn eines
             # Extension-Blocks
             "\xff",  # Application Extension-Signatur
             "\x0b",  # Größe des Blocks (11 Bytes)
             "NETSCAPE2.0",
             "\x03",  # Größe des Sub-Datenblocks (3 Bytes)
             "\x01",
             "\x00\x00",  # Anzahl der Schleifen (0 = unendlich)
             "\x00",  # Ende des Blocks
         ]
         # self.fo.write(''.join(header))
         # Erstes Frame
         for string in GifImagePlugin.getdata(other):
             self.fo.write(string)
     else:
         # Delta-Frame
         delta = ImageChops.subtract_modulo(other, self.previous)
         bbox = delta.getbbox()
         if bbox:
             for string in GifImagePlugin.getdata(other.crop(bbox), offset=bbox[:2]):
                 self.fo.write(string)
         else:
             # XXX Was macht man in dem Fall?
             pass
     self.previous = other.copy()
     return self
开发者ID:Trundle,项目名称:sandbox,代码行数:44,代码来源:ameise.py

示例7: build_animated_gif

def build_animated_gif(stream, images, delay=0.1):
    """Writes an animated GIF into a stream given an iterator of PIL.Images.

    See http://en.wikipedia.org/wiki/Graphics_Interchange_Format#Animated_GIF.
    """
    # Process the images lazily to avoid unnecessary memory load. The first
    # image is used to build the GIF headers.
    images = (image.convert('P') for image in images)
    image = images.next()

    # Header
    stream.write("GIF89a")

    # Logical Screen Descriptor
    stream.write(struct.pack('<H', image.size[0]))
    stream.write(struct.pack('<H', image.size[1]))
    stream.write("\x87\x00\x00")

    # Palette
    stream.write(GifImagePlugin.getheader(image)[1])

    # Application Extension
    stream.write("\x21\xFF\x0B")
    stream.write("NETSCAPE2.0")
    stream.write("\x03\x01")
    stream.write(struct.pack('<H', 2**16-1))
    stream.write('\x00')

    for im in itertools.chain([image], images):
        # Graphic Control Extension
        stream.write('\x21\xF9\x04')
        stream.write('\x08')
        stream.write(struct.pack('<H', int(round(delay*100))))
        stream.write('\x00\x00')

        data = GifImagePlugin.getdata(im)
        for d in data:
            stream.write(d)

    # GIF file terminator
    stream.write(";")
开发者ID:FerHarris,项目名称:google-app-engine-samples,代码行数:41,代码来源:mandelbrot_animation.py

示例8: _open

 def _open(self):
     # Screen
     s = self.fp.read(13)
     if s[:6] not in ["GIF87a", "GIF89a"]:
         raise SyntaxError, "not a GIF file"
     self.info["version"] = s[:6]
     self.size = GifImagePlugin.i16(s[6:]), GifImagePlugin.i16(s[8:])
     self.tile = []
     flags = ord(s[10])
     bits = (flags & 7) + 1
     if flags & 128:
         # get global palette
         self.info["background"] = ord(s[11])
         # check if palette contains colour indices
         p = self.fp.read(3<<bits)
         for i in range(0, len(p), 3):
             if not (chr(i/3) == p[i] == p[i+1] == p[i+2]):
                 p = ImagePalette.raw("RGB", p)
                 self.global_palette = self.palette = p
                 break
     self.__fp = self.fp
     self.__rewind = self.fp.tell()
     self.seek(0) # get ready to read first frame
开发者ID:raydeal,项目名称:animGIF_PIL,代码行数:23,代码来源:ExtGifImagePlugin.py

示例9: make_animated_gif

def make_animated_gif(stream, images, delays):
    '''
    Adapted from https://github.com/GoogleCloudPlatform/appengine-mandelbrot-python/blob/master/mandelbrot_animation.py
    '''
    image = images[0]

    # Header
    stream.write("GIF89a")

    # Logical Screen Descriptor
    stream.write(struct.pack('<H', image.size[0]))
    stream.write(struct.pack('<H', image.size[1]))
    stream.write("\x87\x00\x00")

    # Palette
    stream.write(GifImagePlugin.getheader(image)[1])

    # Application Extension
    stream.write("\x21\xFF\x0B")
    stream.write("NETSCAPE2.0")
    stream.write("\x03\x01")
    stream.write(struct.pack('<H', 2 ** 16 - 1))
    stream.write('\x00')

    for i in xrange(1, len(images)):
        # Graphic Control Extension
        stream.write('\x21\xF9\x04')
        stream.write('\x08')
        stream.write(struct.pack('<H', delays[i]))
        stream.write('\x00\x00')

        data = GifImagePlugin.getdata(images[i])
        for d in data:
            stream.write(d)

    # GIF file terminator
    stream.write(";")
开发者ID:arunpattnaik,项目名称:dun-dun-duh,代码行数:37,代码来源:direct.py

示例10: test_save_netpbm_l_mode

    def test_save_netpbm_l_mode(self):
        img = Image.open(TEST_GIF).convert("L")

        tempfile = self.tempfile("temp.gif")
        GifImagePlugin._save_netpbm(img, 0, tempfile)
        self.assert_image_similar(img, Image.open(tempfile).convert("L"), 0)
开发者ID:EEdie,项目名称:Pillow,代码行数:6,代码来源:test_file_gif.py

示例11: seek

    def seek(self, frame):
        if frame == 0:
            # rewind
            self.__offset = 0
            self.dispose = None
            self.__frame = -1
            self.__fp.seek(self.__rewind)
        if frame != self.__frame + 1:
            raise ValueError, "cannot seek to frame %d" % frame
        self.__frame = frame
        self.tile = []
        self.fp = self.__fp
        if self.__offset:
            # backup to last frame
            self.fp.seek(self.__offset)
            while self.data():
                pass
            self.__offset = 0
        if self.dispose:
            self.im = self.dispose
            self.dispose = None
        self.palette = self.global_palette
        while 1:
            s = self.fp.read(1)
            if not s or s == ";":
                break
            elif s == "!":
                #
                # extensions
                #
                s = self.fp.read(1)
                block = self.data()
                if ord(s) == 249:
                    #
                    # graphic control extension
                    #
                    flags = ord(block[0])
                    if flags & 1:
                        self.info["transparency"] = ord(block[3])
                    self.info["duration"] = GifImagePlugin.i16(block[1:3]) * 10
                    try:
                        # disposal methods
                        if flags & 8:
                            # replace with background colour
                            self.dispose = Image.core.fill("P", self.size,
                                self.info["background"])
                        elif flags & 16:
                            # replace with previous contents
                            self.dispose = self.im.copy()
                    except (AttributeError, KeyError):
                        pass
                elif ord(s) == 255:
                    #
                    # application extension
                    #
                    self.info["extension"] = block, self.fp.tell()
                    if block[:11] == "NETSCAPE2.0":
                        block = self.data()
                        if len(block) >= 3 and ord(block[0]) == 1:
                            self.info["loop"] = GifImagePlugin.i16(block[1:3])
                while self.data():
                    pass
            elif s == ",":
                #
                # local image
                #
                s = self.fp.read(9)
                # extent
                x0, y0 = GifImagePlugin.i16(s[0:]), GifImagePlugin.i16(s[2:])
                x, y = GifImagePlugin.i16(s[4:]), GifImagePlugin.i16(s[6:])
                x1, y1 = x0 + x, y0 + y
                self.info['frame_box'] = (x0,y0,x1,y1)
                flags = ord(s[8])
                interlace = (flags & 64) != 0
                if flags & 128:
                    bits = (flags & 7) + 1
                    self.palette =\
                        ImagePalette.raw("RGB", self.fp.read(3<<bits))

                # image data
                bits = ord(self.fp.read(1))
                self.__offset = self.fp.tell()
                self.tile = [("gif",
                             (x0, y0, x1, y1),
                             self.__offset,
                             (bits, interlace))]
                break
            else:
                pass
                # raise IOError, "illegal GIF tag `%x`" % ord(s)
        if not self.tile:
            # self.__fp = None
            raise EOFError, "no more images in GIF file"
        self.mode = "L"
        if self.palette:
            self.mode = "P"
开发者ID:raydeal,项目名称:animGIF_PIL,代码行数:96,代码来源:ExtGifImagePlugin.py


注:本文中的PIL.GifImagePlugin类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。