#!/usr/bin/env python # Paul Sladen, 2011-12-01 from PIL import PngImagePlugin, Image, ImageFile #Image.DEBUG = True # Meh, why doesn't PIL support this already? def chunk_iTXt(self, pos, len): s = ImageFile._safe_read(self.fp, len) # http://www.w3.org/TR/PNG/#11iTXt k, structure = s.split('\0', 1) compression_flag, compression_method = map(ord, structure[:2]) language_tag, translated, t = structure[2:].split('\0', 2) if compression_flag == 0: v = t.decode('utf-8') elif compression_flag == 1 and compression_method == 0: import zlib v = zlib.decompress(t).decode('utf-8') else: raise SyntaxError("Unknown compression request %d in iTXt chunk" % compression_method) self.im_info[k] = {'translated_keyword': translated.decode('utf-8'), 'language_tag': language_tag, 'text': v} self.im_text[k] = v return s PngImagePlugin.PngStream.chunk_iTXt = chunk_iTXt def test(f): i = PngImagePlugin.PngImageFile(f) print i.info, i.text steno = i.text['Comment'].decode('base64') if __name__ == '__main__': test('cyber.png')