Python PIL.PngImagePlugin.PngInfo() Examples

The following are 8 code examples of PIL.PngImagePlugin.PngInfo(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PIL.PngImagePlugin , or try the search function .
Example #1
Source File: test_file_png.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def test_roundtrip_itxt(self):
        # Check iTXt roundtripping

        im = Image.new("RGB", (32, 32))
        info = PngImagePlugin.PngInfo()
        info.add_itxt("spam", "Eggs", "en", "Spam")
        info.add_text("eggs", PngImagePlugin.iTXt("Spam", "en", "Eggs"),
                      zip=True)

        im = roundtrip(im, pnginfo=info)
        self.assertEqual(im.info, {"spam": "Eggs", "eggs": "Spam"})
        self.assertEqual(im.text, {"spam": "Eggs", "eggs": "Spam"})
        self.assertEqual(im.text["spam"].lang, "en")
        self.assertEqual(im.text["spam"].tkey, "Spam")
        self.assertEqual(im.text["eggs"].lang, "en")
        self.assertEqual(im.text["eggs"].tkey, "Eggs") 
Example #2
Source File: test_file_png.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_unicode_text(self):
        # Check preservation of non-ASCII characters on Python 3
        # This cannot really be meaningfully tested on Python 2,
        # since it didn't preserve charsets to begin with.

        def rt_text(value):
            im = Image.new("RGB", (32, 32))
            info = PngImagePlugin.PngInfo()
            info.add_text("Text", value)
            im = roundtrip(im, pnginfo=info)
            self.assertEqual(im.info, {"Text": value})

        if py3:
            rt_text(" Aa" + chr(0xa0) + chr(0xc4) + chr(0xff))  # Latin1
            rt_text(chr(0x400) + chr(0x472) + chr(0x4ff))       # Cyrillic
            rt_text(chr(0x4e00) + chr(0x66f0) +                 # CJK
                    chr(0x9fba) + chr(0x3042) + chr(0xac00))
            rt_text("A" + chr(0xc4) + chr(0x472) + chr(0x3042))  # Combined 
Example #3
Source File: check_png_dos.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_dos_total_memory(self):
        im = Image.new('L', (1, 1))
        compressed_data = zlib.compress(b'a'*1024*1023)

        info = PngImagePlugin.PngInfo()

        for x in range(64):
            info.add_text('t%s' % x, compressed_data, zip=True)
            info.add_itxt('i%s' % x, compressed_data, zip=True)

        b = BytesIO()
        im.save(b, 'PNG', pnginfo=info)
        b.seek(0)

        try:
            im2 = Image.open(b)
        except ValueError as msg:
            self.assertIn("Too much memory", msg)
            return

        total_len = 0
        for txt in im2.text.values():
            total_len += len(txt)
        self.assertLess(total_len, 64*1024*1024,
                        "Total text chunks greater than 64M") 
Example #4
Source File: test_file_png.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_roundtrip_text(self):
        # Check text roundtripping

        im = Image.open(TEST_PNG_FILE)

        info = PngImagePlugin.PngInfo()
        info.add_text("TXT", "VALUE")
        info.add_text("ZIP", "VALUE", zip=True)

        im = roundtrip(im, pnginfo=info)
        self.assertEqual(im.info, {'TXT': 'VALUE', 'ZIP': 'VALUE'})
        self.assertEqual(im.text, {'TXT': 'VALUE', 'ZIP': 'VALUE'}) 
Example #5
Source File: test_file_png.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_nonunicode_text(self):
        # Check so that non-Unicode text is saved as a tEXt rather than iTXt

        im = Image.new("RGB", (32, 32))
        info = PngImagePlugin.PngInfo()
        info.add_text("Text", "Ascii")
        im = roundtrip(im, pnginfo=info)
        self.assertIsInstance(im.info["Text"], str) 
Example #6
Source File: png.py    From convis with GNU General Public License v3.0 5 votes vote down vote up
def pngsave(A, file, info={}):
    """
        wrapper around PIL 1.1.7 Image.save to preserve PNG metadata
        based on public domain script by Nick Galbreath                                                                                                        

        http://blog.modp.com/2007/08/python-pil-and-png-metadata-take-2.html                                                                 
    """
    from PIL import Image, PngImagePlugin
    im = Image.fromarray(256.0*A).convert('RGB')
    reserved = ('interlace', 'gamma', 'dpi', 'transparency', 'aspect')
    meta = PngImagePlugin.PngInfo()
    for k,v in _iteritems(info):
        if k in reserved: continue
        meta.add_text(k, v, 0)
    im.save(file, "PNG", pnginfo=meta) 
Example #7
Source File: png.py    From convis with GNU General Public License v3.0 5 votes vote down vote up
def png_client(images,info={},port=10000,host='localhost',compress_level=0,resize=(1.0,1.0)):
    if len(images.shape) == 2:
        images = [images]
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect the socket to the port where the server is listening
    server_address = (host, port)
    sock.connect(server_address)
    try:
        for A in images:
            im = Image.fromarray(256.0*A).convert('RGB')
            if resize != (1.0,1.0) and resize is not None:
                if not type(resize) == tuple:
                    resize = (resize,resize)
                im = im.resize((int(resize[0]*im.size[0]), int(resize[1]*im.size[1])), PIL.Image.ANTIALIAS)
            output = StringIO.StringIO()
            meta = PngImagePlugin.PngInfo()
            reserved = ('interlace', 'gamma', 'dpi', 'transparency', 'aspect')
            for k,v in _iteritems(info):
                if k in reserved:
                    continue
                meta.add_text(str(k), str(v), 0)
            im.save(output, format="PNG", pnginfo=meta, compress_level=compress_level)
            message = output.getvalue()
            output.close()
            sock.sendall(message)
    finally:
        sock.close() 
Example #8
Source File: metadata.py    From pycbc with GNU General Public License v3.0 5 votes vote down vote up
def save_png_with_metadata(fig, filename, fig_kwds, kwds):
    """ Save a matplotlib figure to a png with metadata
    """
    from PIL import Image, PngImagePlugin
    fig.savefig(filename, **fig_kwds)

    im = Image.open(filename)
    meta = PngImagePlugin.PngInfo()

    for key in kwds:
        meta.add_text(str(key), str(kwds[key]))

    im.save(filename, "png", pnginfo=meta)