fix for ticket #404
[mediagoblin.git] / mediagoblin / media_types / image / processing.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
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.
8 #
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.
13 #
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/>.
16
17 import Image
18 import os
19
20 from mediagoblin import mg_globals as mgg
21 from mediagoblin.processing import BadMediaFail, \
22 create_pub_filepath, THUMB_SIZE, MEDIUM_SIZE
23 from mediagoblin.tools.exif import exif_fix_image_orientation, \
24 extract_exif, clean_exif, get_gps_data, get_useful, \
25 exif_image_needs_rotation
26
27 def process_image(entry):
28 """
29 Code to process an image
30 """
31 workbench = mgg.workbench_manager.create_workbench()
32 # Conversions subdirectory to avoid collisions
33 conversions_subdir = os.path.join(
34 workbench.dir, 'conversions')
35 os.mkdir(conversions_subdir)
36 queued_filepath = entry.queued_media_file
37 queued_filename = workbench.localized_file(
38 mgg.queue_store, queued_filepath,
39 'source')
40
41 filename_bits = os.path.splitext(queued_filename)
42 basename = os.path.split(filename_bits[0])[1]
43 extension = filename_bits[1].lower()
44
45 # EXIF extraction
46 exif_tags = extract_exif(queued_filename)
47 gps_data = get_gps_data(exif_tags)
48
49 try:
50 thumb = Image.open(queued_filename)
51 except IOError:
52 raise BadMediaFail()
53
54 thumb = exif_fix_image_orientation(thumb, exif_tags)
55
56 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
57
58 # Copy the thumb to the conversion subdir, then remotely.
59 thumb_filename = 'thumbnail' + extension
60 thumb_filepath = create_pub_filepath(entry, thumb_filename)
61
62 tmp_thumb_filename = os.path.join(
63 conversions_subdir, thumb_filename)
64
65 with file(tmp_thumb_filename, 'w') as thumb_file:
66 thumb.save(thumb_file)
67
68 mgg.public_store.copy_local_to_storage(
69 tmp_thumb_filename, thumb_filepath)
70
71 # If the size of the original file exceeds the specified size of a `medium`
72 # file, a `medium.jpg` files is created and later associated with the media
73 # entry.
74 medium = Image.open(queued_filename)
75 if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1] \
76 or exif_image_needs_rotation(exif_tags):
77
78 medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
79
80
81
82 # Fix orientation
83 medium = exif_fix_image_orientation(medium, exif_tags)
84
85
86
87 medium_filename = 'medium' + extension
88 medium_filepath = create_pub_filepath(entry, medium_filename)
89
90 tmp_medium_filename = os.path.join(
91 conversions_subdir, medium_filename)
92
93 with file(tmp_medium_filename, 'w') as medium_file:
94 medium.save(medium_file)
95
96 mgg.public_store.copy_local_to_storage(
97 tmp_medium_filename, medium_filepath)
98 else:
99 medium_filepath = None
100
101 # we have to re-read because unlike PIL, not everything reads
102 # things in string representation :)
103 queued_file = file(queued_filename, 'rb')
104
105 with queued_file:
106 #create_pub_filepath(entry, queued_filepath[-1])
107 original_filepath = create_pub_filepath(entry, basename + extension)
108
109 with mgg.public_store.get_file(original_filepath, 'wb') \
110 as original_file:
111 original_file.write(queued_file.read())
112
113 # Remove queued media file from storage and database
114 mgg.queue_store.delete_file(queued_filepath)
115 entry.queued_media_file = []
116
117 # Insert media file information into database
118 media_files_dict = entry.setdefault('media_files', {})
119 media_files_dict['thumb'] = thumb_filepath
120 media_files_dict['original'] = original_filepath
121 if medium_filepath:
122 media_files_dict['medium'] = medium_filepath
123
124 # Insert exif data into database
125 media_data = entry.setdefault('media_data', {})
126 media_data['exif'] = {
127 'clean': clean_exif(exif_tags)}
128 media_data['exif']['useful'] = get_useful(
129 media_data['exif']['clean'])
130 media_data['gps'] = gps_data
131
132 # clean up workbench
133 workbench.destroy_self()
134
135 if __name__ == '__main__':
136 import sys
137 import pprint
138
139 pp = pprint.PrettyPrinter()
140
141 result = extract_exif(sys.argv[1])
142 gps = get_gps_data(result)
143 clean = clean_exif(result)
144 useful = get_useful(clean)
145
146 print pp.pprint(
147 clean)