1 | #if 0 |
---|
2 | # ----------------------------------------------------------------------- |
---|
3 | # $Id: IPTC.py,v 1.9 2004/05/04 22:03:17 dischi Exp $ |
---|
4 | # ----------------------------------------------------------------------- |
---|
5 | # $Log: IPTC.py,v $ |
---|
6 | # Revision 1.9 2004/05/04 22:03:17 dischi |
---|
7 | # handle bad jpeg |
---|
8 | # |
---|
9 | # Revision 1.8 2003/06/09 16:11:57 the_krow |
---|
10 | # TIFF parser changed to new tables structure |
---|
11 | # debug statements removed / changed to _debug |
---|
12 | # |
---|
13 | # Revision 1.7 2003/06/07 21:48:47 the_krow |
---|
14 | # Added Copying info |
---|
15 | # started changing riffinfo to new AV stuff |
---|
16 | # |
---|
17 | # Revision 1.6 2003/05/13 17:49:41 the_krow |
---|
18 | # IPTC restructured |
---|
19 | # EXIF Height read correctly |
---|
20 | # JPEG Endmarker read |
---|
21 | # |
---|
22 | # Revision 1.5 2003/05/13 15:23:59 the_krow |
---|
23 | # IPTC |
---|
24 | # |
---|
25 | # Revision 1.4 2003/05/13 15:16:02 the_krow |
---|
26 | # width+height hacked |
---|
27 | # |
---|
28 | # Revision 1.3 2003/05/13 15:00:23 the_krow |
---|
29 | # Tiff parsing |
---|
30 | # |
---|
31 | # |
---|
32 | # ----------------------------------------------------------------------- |
---|
33 | # MMPython - Media Metadata for Python |
---|
34 | # Copyright (C) 2003 Thomas Schueppel |
---|
35 | # |
---|
36 | # This program is free software; you can redistribute it and/or modify |
---|
37 | # it under the terms of the GNU General Public License as published by |
---|
38 | # the Free Software Foundation; either version 2 of the License, or |
---|
39 | # (at your option) any later version. |
---|
40 | # |
---|
41 | # This program is distributed in the hope that it will be useful, but |
---|
42 | # WITHOUT ANY WARRANTY; without even the implied warranty of MER- |
---|
43 | # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
44 | # Public License for more details. |
---|
45 | # |
---|
46 | # You should have received a copy of the GNU General Public License along |
---|
47 | # with this program; if not, write to the Free Software Foundation, Inc., |
---|
48 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
49 | # |
---|
50 | # ----------------------------------------------------------------------- |
---|
51 | #endif |
---|
52 | |
---|
53 | # http://www.ap.org/apserver/userguide/codes.htm |
---|
54 | |
---|
55 | from struct import unpack |
---|
56 | |
---|
57 | def flatten(list): |
---|
58 | try: |
---|
59 | for i in list.keys(): |
---|
60 | val = list[i] |
---|
61 | if len(val) == 0: list[i] = None |
---|
62 | elif len(val) == 1: list[i] = val[0] |
---|
63 | else: list[i] = tuple(val) |
---|
64 | return list |
---|
65 | except: |
---|
66 | return [] |
---|
67 | |
---|
68 | def parseiptc(app): |
---|
69 | iptc = {} |
---|
70 | if app[:14] == "Photoshop 3.0\x00": |
---|
71 | app = app[14:] |
---|
72 | if 1: |
---|
73 | # parse the image resource block |
---|
74 | offset = 0 |
---|
75 | data = None |
---|
76 | while app[offset:offset+4] == "8BIM": |
---|
77 | offset = offset + 4 |
---|
78 | # resource code |
---|
79 | code = unpack("<H", app[offset:offset+2])[0] |
---|
80 | offset = offset + 2 |
---|
81 | # resource name (usually empty) |
---|
82 | name_len = ord(app[offset]) |
---|
83 | name = app[offset+1:offset+1+name_len] |
---|
84 | offset = 1 + offset + name_len |
---|
85 | if offset & 1: |
---|
86 | offset = offset + 1 |
---|
87 | # resource data block |
---|
88 | size = unpack("<L", app[offset:offset+4])[0] |
---|
89 | offset = offset + 4 |
---|
90 | if code == 0x0404: |
---|
91 | # 0x0404 contains IPTC/NAA data |
---|
92 | data = app[offset:offset+size] |
---|
93 | break |
---|
94 | offset = offset + size |
---|
95 | if offset & 1: |
---|
96 | offset = offset + 1 |
---|
97 | if not data: |
---|
98 | return None |
---|
99 | offset = 0 |
---|
100 | iptc = {} |
---|
101 | while 1: |
---|
102 | try: |
---|
103 | intro = ord(data[offset]) |
---|
104 | except IndexError: |
---|
105 | return '' |
---|
106 | if intro != 0x1c: |
---|
107 | return iptc |
---|
108 | (key,len) = unpack('>HH',data[offset+1:offset+5]) |
---|
109 | val = data[offset+5:offset+len+5] |
---|
110 | if iptc.has_key(key): |
---|
111 | iptc[key].append(val) |
---|
112 | else: |
---|
113 | iptc[key] = [val] |
---|
114 | offset += len + 5 |
---|
115 | return iptc |
---|
116 | |
---|
117 | |
---|