source: trunk/src/testing/bin/fileServer/misc/mmpython/image/jpginfo.py @ 4

Revision 4, 7.2 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1#if 0
2# -----------------------------------------------------------------------
3# $Id: jpginfo.py,v 1.21 2005/04/16 17:08:40 dischi Exp $
4# -----------------------------------------------------------------------
5# $Log: jpginfo.py,v $
6# Revision 1.21  2005/04/16 17:08:40  dischi
7# read metadata from epeg
8#
9# Revision 1.20  2005/04/16 15:01:15  dischi
10# convert exif tags to str
11#
12# Revision 1.19  2004/05/20 15:56:31  dischi
13# use Python Imaging for more info and gif/bmp support
14#
15# Revision 1.18  2003/06/30 13:17:19  the_krow
16# o Refactored mediainfo into factory, synchronizedobject
17# o Parsers now register directly at mmpython not at mmpython.mediainfo
18# o use mmpython.Factory() instead of mmpython.mediainfo.get_singleton()
19# o Bugfix in PNG parser
20# o Renamed disc.AudioInfo into disc.AudioDiscInfo
21# o Renamed disc.DataInfo into disc.DataDiscInfo
22#
23# Revision 1.17  2003/06/20 19:17:22  dischi
24# remove filename again and use file.name
25#
26# Revision 1.16  2003/06/10 13:02:32  the_krow
27# Softened JPEG parser a little so it accepts jpegs that do not end in FFD9.
28#
29# Revision 1.15  2003/06/09 14:31:57  the_krow
30# fixes on the mpeg parser
31# resolutions, fps and bitrate should be reported correctly now
32#
33# Revision 1.14  2003/06/09 12:50:08  the_krow
34# mp3 now fills tables
35#
36# Revision 1.13  2003/06/08 19:55:22  dischi
37# added bins metadata support
38#
39# Revision 1.12  2003/06/08 16:48:08  dischi
40# Changed self.exif to exif_info and the same for ipc. We should extract
41# everything we need to self.xxx and don't remember where the info came
42# from (it's bad for the cache, we cache everything twice).
43# Also added thumbnail to the list of things we want
44#
45# Revision 1.11  2003/06/08 13:44:57  dischi
46# Changed all imports to use the complete mmpython path for mediainfo
47#
48# Revision 1.10  2003/06/08 13:11:51  dischi
49# removed print at the end and moved it into register
50#
51# Revision 1.9  2003/06/07 21:48:47  the_krow
52# Added Copying info
53# started changing riffinfo to new AV stuff
54#
55# Revision 1.8  2003/05/13 18:28:17  the_krow
56# JPEG Resolution
57#
58# Revision 1.7  2003/05/13 17:49:41  the_krow
59# IPTC restructured\nEXIF Height read correctly\nJPEG Endmarker read
60#
61# Revision 1.6  2003/05/13 15:52:42  the_krow
62# Caption added
63#
64# Revision 1.5  2003/05/13 15:23:59  the_krow
65# IPTC
66#
67# Revision 1.4  2003/05/13 15:00:23  the_krow
68# Tiff parsing
69#
70# Revision 1.3  2003/05/13 12:31:11  the_krow
71# + GNU Copyright Notice
72# + PNG Parsing
73#
74# -----------------------------------------------------------------------
75# MMPython - Media Metadata for Python
76# Copyright (C) 2003 Thomas Schueppel
77#
78# This program is free software; you can redistribute it and/or modify
79# it under the terms of the GNU General Public License as published by
80# the Free Software Foundation; either version 2 of the License, or
81# (at your option) any later version.
82#
83# This program is distributed in the hope that it will be useful, but
84# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
85# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
86# Public License for more details.
87#
88# You should have received a copy of the GNU General Public License along
89# with this program; if not, write to the Free Software Foundation, Inc.,
90# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
91#
92# -----------------------------------------------------------------------
93#endif
94
95
96from mmpython import mediainfo
97import mmpython
98import IPTC
99import EXIF
100import struct
101
102import ImageInfo
103
104# interesting file format info:
105# http://www.dcs.ed.ac.uk/home/mxr/gfx/2d-hi.html
106# http://www.funducode.com/freec/Fileformats/format3/format3b.htm
107
108SOF = { 0xC0 : "Baseline",   
109        0xC1 : "Extended sequential",   
110        0xC2 : "Progressive",   
111        0xC3 : "Lossless",   
112        0xC5 : "Differential sequential",   
113        0xC6 : "Differential progressive",   
114        0xC7 : "Differential lossless",   
115        0xC9 : "Extended sequential, arithmetic coding",   
116        0xCA : "Progressive, arithmetic coding",   
117        0xCB : "Lossless, arithmetic coding",   
118        0xCD : "Differential sequential, arithmetic coding",   
119        0xCE : "Differential progressive, arithmetic coding",   
120        0xCF : "Differential lossless, arithmetic coding",
121}
122
123_debug = mediainfo._debug
124
125class JPGInfo(mediainfo.ImageInfo):
126
127    def __init__(self,file):
128        mediainfo.ImageInfo.__init__(self)
129        iptc_info = None       
130        self.mime = 'image/jpeg'
131        self.type = 'jpeg image'
132        self.valid = 1
133        if file.read(2) != '\xff\xd8':
134            self.valid = 0
135            return
136        file.seek(-2,2)
137        if file.read(2) != '\xff\xd9':
138            # Normally an JPEG should end in ffd9. This does not however
139            # we assume it's an jpeg for now
140            mediainfo._debug("Wrong encode found for jpeg")
141        file.seek(2)
142        app = file.read(4)
143        self.meta = {}
144        while (len(app) == 4):
145            (ff,segtype,seglen) = struct.unpack(">BBH", app)
146            if ff != 0xff: break
147            _debug("SEGMENT: 0x%x%x, len=%d" % (ff,segtype,seglen))
148            if segtype == 0xd9:
149                break
150            elif SOF.has_key(segtype):
151                data = file.read(seglen-2)
152                (precision,self.height,self.width,num_comp) = struct.unpack('>BHHB', data[:6])
153                #_debug("H/W: %i / %i" % (self.height, self.width))
154            elif segtype == 0xed:
155                app = file.read(seglen-2)
156                iptc_info = IPTC.flatten(IPTC.parseiptc(app))
157                break
158            elif segtype == 0xe7:
159                # information created by libs like epeg
160                data = file.read(seglen-2)
161                if data.count('\n') == 1:
162                    key, value = data.split('\n')
163                    self.meta[key] = value
164            else:
165                file.seek(seglen-2,1)
166            app = file.read(4)
167        file.seek(0)
168        exif_info = EXIF.process_file(file)
169        if exif_info:
170            self.setitem( 'date', exif_info, 'Image DateTime', True )           
171            self.setitem( 'artist', exif_info, 'Image Artist', True )
172            self.setitem( 'hardware', exif_info, 'Image Model', True )
173            self.setitem( 'software', exif_info, 'Image Software', True )
174            self.setitem( 'thumbnail', exif_info, 'JPEGThumbnail', True )
175            self.appendtable( 'EXIF', exif_info )
176        if iptc_info:
177            self.setitem( 'title', iptc_info, 517, True )
178            self.setitem( 'date' , iptc_info, 567, True )
179            self.setitem( 'comment', iptc_info, 617, True )
180            self.setitem( 'keywords', iptc_info, 537, True )
181            self.setitem( 'artist', iptc_info, 592, True )
182            self.setitem( 'country', iptc_info, 612, True )
183            self.setitem( 'caption', iptc_info, 632, True )
184            self.appendtable( 'IPTC', iptc_info )           
185
186        if len(self.meta.keys()):
187            self.appendtable( 'JPGMETA', self.meta )
188        for key, value in self.meta.items():
189            if key.startswith('Thumb:') or key == 'Software':
190                setattr(self, key, value)
191                if not key in self.keys:
192                    self.keys.append(key)
193
194        ImageInfo.add(file.name, self)
195        return
196       
197
198mmpython.registertype( 'image/jpeg', ('jpg','jpeg'), mediainfo.TYPE_IMAGE, JPGInfo )
Note: See TracBrowser for help on using the repository browser.