c4fc1fe5a5b5bce0422df68da0471c5644f79432
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from mediagoblin
.tools
.extlib
.EXIF
import process_file
, Ratio
18 from mediagoblin
.processing
import BadMediaFail
19 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
21 # A list of tags that should be stored for faster access
31 'EXIF ISOSpeedRatings',
36 def exif_image_needs_rotation(exif_tags
):
38 Returns True if EXIF orientation requires rotation
40 return 'Image Orientation' in exif_tags \
41 and exif_tags
['Image Orientation'].values
[0] != 1
44 def exif_fix_image_orientation(im
, exif_tags
):
46 Translate any EXIF orientation to raw orientation
49 - REDUCES IMAGE QUALITY by recompressig it
55 if 'Image Orientation' in exif_tags
:
60 orientation
= exif_tags
['Image Orientation'].values
[0]
61 if orientation
in rotation_map
.keys():
63 rotation_map
[orientation
])
68 def extract_exif(filename
):
70 Returns EXIF tags found in file at ``filename``
75 image
= open(filename
)
76 exif_tags
= process_file(image
)
78 raise BadMediaFail(_('Could not read the image file.'))
85 Clean the result from anything the database cannot handle
87 # Discard any JPEG thumbnail, for database compatibility
88 # and that I cannot see a case when we would use it.
89 # It takes up some space too.
91 'Thumbnail JPEGInterchangeFormatLength',
93 'Thumbnail JPEGInterchangeFormat']
97 for key
, value
in exif
.items():
98 if not key
in disabled_tags
:
99 clean_exif
[key
] = _ifd_tag_to_dict(value
)
104 def _ifd_tag_to_dict(tag
):
106 'printable': tag
.printable
,
108 'field_type': tag
.field_type
,
109 'field_offset': tag
.field_offset
,
110 'field_length': tag
.field_length
,
112 if type(tag
.values
) == list:
114 for val
in tag
.values
:
115 if isinstance(val
, Ratio
):
116 data
['values'].append(
119 data
['values'].append(val
)
121 data
['values'] = tag
.values
126 def _ratio_to_list(ratio
):
127 return [ratio
.num
, ratio
.den
]
130 def get_useful(tags
):
132 for key
, tag
in tags
.items():
133 if key
in USEFUL_TAGS
:
139 def get_gps_data(tags
):
141 Processes EXIF data returned by EXIF.py
145 if not 'Image GPSInfo' in tags
:
150 'latitude': tags
['GPS GPSLatitude'],
151 'longitude': tags
['GPS GPSLongitude']}
153 for key
, dat
in dms_data
.items():
156 float(v
[0].num
) / float(v
[0].den
) \
157 + (float(v
[1].num
) / float(v
[1].den
) / 60) \
158 + (float(v
[2].num
) / float(v
[2].den
) / (60 * 60))
161 if tags
['GPS GPSLongitudeRef'].values
== 'W':
162 gps_data
['longitude'] /= -1
168 gps_data
['direction'] = (
170 float(d
.num
) / float(d
.den
)
171 )(tags
['GPS GPSImgDirection'].values
[0])
176 gps_data
['altitude'] = (
178 float(a
.num
) / float(a
.den
)
179 )(tags
['GPS GPSAltitude'].values
[0])