1 | #if 0 |
---|
2 | # ----------------------------------------------------------------------- |
---|
3 | # $Id: ImageInfo.py,v 1.2 2005/04/16 15:01:53 dischi Exp $ |
---|
4 | # ----------------------------------------------------------------------- |
---|
5 | # $Log: ImageInfo.py,v $ |
---|
6 | # Revision 1.2 2005/04/16 15:01:53 dischi |
---|
7 | # read gthumb comment files |
---|
8 | # |
---|
9 | # Revision 1.1 2004/05/20 15:56:31 dischi |
---|
10 | # use Python Imaging for more info and gif/bmp support |
---|
11 | # |
---|
12 | # |
---|
13 | # ----------------------------------------------------------------------- |
---|
14 | # MMPython - Media Metadata for Python |
---|
15 | # Copyright (C) 2003 Thomas Schueppel |
---|
16 | # |
---|
17 | # This program is free software; you can redistribute it and/or modify |
---|
18 | # it under the terms of the GNU General Public License as published by |
---|
19 | # the Free Software Foundation; either version 2 of the License, or |
---|
20 | # (at your option) any later version. |
---|
21 | # |
---|
22 | # This program is distributed in the hope that it will be useful, but |
---|
23 | # WITHOUT ANY WARRANTY; without even the implied warranty of MER- |
---|
24 | # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
25 | # Public License for more details. |
---|
26 | # |
---|
27 | # You should have received a copy of the GNU General Public License along |
---|
28 | # with this program; if not, write to the Free Software Foundation, Inc., |
---|
29 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
30 | # |
---|
31 | # ----------------------------------------------------------------------- |
---|
32 | #endif |
---|
33 | |
---|
34 | import mmpython |
---|
35 | from mmpython import mediainfo |
---|
36 | |
---|
37 | import os |
---|
38 | import gzip |
---|
39 | #from xml.utils import qp_xml #this doesn't exist for some reason |
---|
40 | import xml.parsers.expat as qp_xml |
---|
41 | |
---|
42 | DEBUG = mediainfo.DEBUG |
---|
43 | |
---|
44 | try: |
---|
45 | import Image |
---|
46 | except: |
---|
47 | if DEBUG: |
---|
48 | print 'Python Imaging not found' |
---|
49 | |
---|
50 | import bins |
---|
51 | |
---|
52 | def add(filename, object): |
---|
53 | if os.path.isfile(filename + '.xml'): |
---|
54 | try: |
---|
55 | binsinfo = bins.get_bins_desc(filename) |
---|
56 | # get needed keys from exif infos |
---|
57 | for key in mediainfo.IMAGECORE + mediainfo.MEDIACORE: |
---|
58 | if not object[key] and binsinfo['exif'].has_key(key): |
---|
59 | object[key] = binsinfo['exif'][key] |
---|
60 | # get _all_ infos from description |
---|
61 | for key in binsinfo['desc']: |
---|
62 | object[key] = binsinfo['desc'][key] |
---|
63 | if not key in mediainfo.IMAGECORE + mediainfo.MEDIACORE: |
---|
64 | # if it's in desc it must be important |
---|
65 | object.keys.append(key) |
---|
66 | except Exception, e: |
---|
67 | if DEBUG: |
---|
68 | print e |
---|
69 | pass |
---|
70 | |
---|
71 | comment_file = os.path.join(os.path.dirname(filename), |
---|
72 | '.comments', |
---|
73 | os.path.basename(filename) + '.xml') |
---|
74 | if os.path.isfile(comment_file): |
---|
75 | try: |
---|
76 | f = gzip.open(comment_file) |
---|
77 | p = qp_xml.Parser() |
---|
78 | tree = p.parse(f) |
---|
79 | f.close() |
---|
80 | for c in tree.children: |
---|
81 | if c.name == 'Place': |
---|
82 | object.location = c.textof() |
---|
83 | if c.name == 'Note': |
---|
84 | object.description = c.textof() |
---|
85 | except: |
---|
86 | pass |
---|
87 | try: |
---|
88 | i = Image.open(filename) |
---|
89 | except: |
---|
90 | return 0 |
---|
91 | |
---|
92 | if not object.mime: |
---|
93 | object.mime = 'image/%s' % i.format.lower() |
---|
94 | |
---|
95 | object.type = i.format_description |
---|
96 | |
---|
97 | if i.info.has_key('dpi'): |
---|
98 | object['dpi'] = '%sx%s' % i.info['dpi'] |
---|
99 | |
---|
100 | if DEBUG: |
---|
101 | for info in i.info: |
---|
102 | if not info == 'exif': |
---|
103 | print '%s: %s' % (info, i.info[info]) |
---|
104 | |
---|
105 | object.mode = i.mode |
---|
106 | if not object.height: |
---|
107 | object.width, object.height = i.size |
---|
108 | |
---|
109 | return 1 |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | class ImageInfo(mediainfo.ImageInfo): |
---|
116 | |
---|
117 | def __init__(self,file): |
---|
118 | mediainfo.ImageInfo.__init__(self) |
---|
119 | self.mime = '' |
---|
120 | self.type = '' |
---|
121 | self.valid = add(file.name, self) |
---|
122 | |
---|
123 | mmpython.registertype( 'image/gif', ('gif',), mediainfo.TYPE_IMAGE, ImageInfo ) |
---|
124 | mmpython.registertype( 'image/bmp', ('bmp',), mediainfo.TYPE_IMAGE, ImageInfo ) |
---|