[4] | 1 | #if 0 |
---|
| 2 | # ----------------------------------------------------------------------- |
---|
| 3 | # $Id: pnginfo.py,v 1.11 2005/04/16 17:09:12 dischi Exp $ |
---|
| 4 | # ----------------------------------------------------------------------- |
---|
| 5 | # $Log: pnginfo.py,v $ |
---|
| 6 | # Revision 1.11 2005/04/16 17:09:12 dischi |
---|
| 7 | # add thumbnail data to internal vars |
---|
| 8 | # |
---|
| 9 | # Revision 1.10 2004/05/20 15:56:31 dischi |
---|
| 10 | # use Python Imaging for more info and gif/bmp support |
---|
| 11 | # |
---|
| 12 | # Revision 1.9 2003/06/30 13:17:20 the_krow |
---|
| 13 | # o Refactored mediainfo into factory, synchronizedobject |
---|
| 14 | # o Parsers now register directly at mmpython not at mmpython.mediainfo |
---|
| 15 | # o use mmpython.Factory() instead of mmpython.mediainfo.get_singleton() |
---|
| 16 | # o Bugfix in PNG parser |
---|
| 17 | # o Renamed disc.AudioInfo into disc.AudioDiscInfo |
---|
| 18 | # o Renamed disc.DataInfo into disc.DataDiscInfo |
---|
| 19 | # |
---|
| 20 | # Revision 1.8 2003/06/23 20:59:11 the_krow |
---|
| 21 | # PNG should now fill a correct table. |
---|
| 22 | # |
---|
| 23 | # Revision 1.7 2003/06/20 19:17:22 dischi |
---|
| 24 | # remove filename again and use file.name |
---|
| 25 | # |
---|
| 26 | # Revision 1.6 2003/06/08 20:27:42 dischi |
---|
| 27 | # small fix for broken files and fixed a wrong bins_data call |
---|
| 28 | # |
---|
| 29 | # Revision 1.5 2003/06/08 19:55:22 dischi |
---|
| 30 | # added bins metadata support |
---|
| 31 | # |
---|
| 32 | # Revision 1.4 2003/06/08 13:44:58 dischi |
---|
| 33 | # Changed all imports to use the complete mmpython path for mediainfo |
---|
| 34 | # |
---|
| 35 | # Revision 1.3 2003/06/08 13:11:51 dischi |
---|
| 36 | # removed print at the end and moved it into register |
---|
| 37 | # |
---|
| 38 | # Revision 1.2 2003/05/13 15:00:23 the_krow |
---|
| 39 | # Tiff parsing |
---|
| 40 | # |
---|
| 41 | # Revision 1.1 2003/05/13 12:31:11 the_krow |
---|
| 42 | # + GNU Copyright Notice |
---|
| 43 | # + PNG Parsing |
---|
| 44 | # |
---|
| 45 | # ----------------------------------------------------------------------- |
---|
| 46 | # MMPython - Media Metadata for Python |
---|
| 47 | # Copyright (C) 2003 Thomas Schueppel |
---|
| 48 | # |
---|
| 49 | # This program is free software; you can redistribute it and/or modify |
---|
| 50 | # it under the terms of the GNU General Public License as published by |
---|
| 51 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 52 | # (at your option) any later version. |
---|
| 53 | # |
---|
| 54 | # This program is distributed in the hope that it will be useful, but |
---|
| 55 | # WITHOUT ANY WARRANTY; without even the implied warranty of MER- |
---|
| 56 | # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
| 57 | # Public License for more details. |
---|
| 58 | # |
---|
| 59 | # You should have received a copy of the GNU General Public License along |
---|
| 60 | # with this program; if not, write to the Free Software Foundation, Inc., |
---|
| 61 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 62 | # |
---|
| 63 | # ----------------------------------------------------------------------- |
---|
| 64 | #endif |
---|
| 65 | |
---|
| 66 | from mmpython import mediainfo |
---|
| 67 | import mmpython |
---|
| 68 | import IPTC |
---|
| 69 | import EXIF |
---|
| 70 | import struct |
---|
| 71 | import zlib |
---|
| 72 | import ImageInfo |
---|
| 73 | |
---|
| 74 | # interesting file format info: |
---|
| 75 | # http://www.libpng.org/pub/png/png-sitemap.html#programming |
---|
| 76 | # http://pmt.sourceforge.net/pngmeta/ |
---|
| 77 | |
---|
| 78 | PNGSIGNATURE = "\211PNG\r\n\032\n" |
---|
| 79 | |
---|
| 80 | _print = mediainfo._debug |
---|
| 81 | |
---|
| 82 | class PNGInfo(mediainfo.ImageInfo): |
---|
| 83 | |
---|
| 84 | def __init__(self,file): |
---|
| 85 | mediainfo.ImageInfo.__init__(self) |
---|
| 86 | self.iptc = None |
---|
| 87 | self.mime = 'image/png' |
---|
| 88 | self.type = 'PNG image' |
---|
| 89 | self.valid = 1 |
---|
| 90 | signature = file.read(8) |
---|
| 91 | if ( signature != PNGSIGNATURE ): |
---|
| 92 | self.valid = 0 |
---|
| 93 | return |
---|
| 94 | self.meta = {} |
---|
| 95 | while self._readChunk(file): |
---|
| 96 | pass |
---|
| 97 | if len(self.meta.keys()): |
---|
| 98 | self.appendtable( 'PNGMETA', self.meta ) |
---|
| 99 | for key, value in self.meta.items(): |
---|
| 100 | if key.startswith('Thumb:') or key == 'Software': |
---|
| 101 | setattr(self, key, value) |
---|
| 102 | if not key in self.keys: |
---|
| 103 | self.keys.append(key) |
---|
| 104 | ImageInfo.add(file.name, self) |
---|
| 105 | return |
---|
| 106 | |
---|
| 107 | def _readChunk(self,file): |
---|
| 108 | try: |
---|
| 109 | (length, type) = struct.unpack('>I4s', file.read(8)) |
---|
| 110 | except: |
---|
| 111 | return 0 |
---|
| 112 | if ( type == 'tEXt' ): |
---|
| 113 | _print('latin-1 Text found.') |
---|
| 114 | (data, crc) = struct.unpack('>%isI' % length,file.read(length+4)) |
---|
| 115 | (key, value) = data.split('\0') |
---|
| 116 | self.meta[key] = value |
---|
| 117 | _print("%s -> %s" % (key,value)) |
---|
| 118 | elif ( type == 'zTXt' ): |
---|
| 119 | _print('Compressed Text found.') |
---|
| 120 | (data,crc) = struct.unpack('>%isI' % length,file.read(length+4)) |
---|
| 121 | split = data.split('\0') |
---|
| 122 | key = split[0] |
---|
| 123 | value = "".join(split[1:]) |
---|
| 124 | compression = ord(value[0]) |
---|
| 125 | value = value[1:] |
---|
| 126 | if compression == 0: |
---|
| 127 | decompressed = zlib.decompress(value) |
---|
| 128 | _print("%s (Compressed %i) -> %s" % (key,compression,decompressed)) |
---|
| 129 | else: |
---|
| 130 | _print("%s has unknown Compression %c" % (key,compression)) |
---|
| 131 | self.meta[key] = value |
---|
| 132 | elif ( type == 'iTXt' ): |
---|
| 133 | _print('International Text found.') |
---|
| 134 | (data,crc) = struct.unpack('>%isI' % length,file.read(length+4)) |
---|
| 135 | (key, value) = data.split('\0') |
---|
| 136 | self.meta[key] = value |
---|
| 137 | _print("%s -> %s" % (key,value)) |
---|
| 138 | else: |
---|
| 139 | file.seek(length+4,1) |
---|
| 140 | _print("%s of length %d ignored." % (type, length)) |
---|
| 141 | return 1 |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | mmpython.registertype( 'image/png', ('png',), mediainfo.TYPE_IMAGE, PNGInfo ) |
---|