Merge remote-tracking branch 'gandaro/forgot-password-autofillin'
[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
26 def process_image(entry):
27 """
28 Code to process an image
29 """
30 workbench = mgg.workbench_manager.create_workbench()
31 # Conversions subdirectory to avoid collisions
32 conversions_subdir = os.path.join(
33 workbench.dir, 'conversions')
34 os.mkdir(conversions_subdir)
35
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
76 # Fix orientation
77 medium = exif_fix_image_orientation(medium, exif_tags)
78
79 if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]:
80 medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
81
82 medium_filename = 'medium' + extension
83 medium_filepath = create_pub_filepath(entry, medium_filename)
84
85 tmp_medium_filename = os.path.join(
86 conversions_subdir, medium_filename)
87
88 with file(tmp_medium_filename, 'w') as medium_file:
89 medium.save(medium_file)
90
91 mgg.public_store.copy_local_to_storage(
92 tmp_medium_filename, medium_filepath)
93
94 # we have to re-read because unlike PIL, not everything reads
95 # things in string representation :)
96 queued_file = file(queued_filename, 'rb')
97
98 with queued_file:
99 #create_pub_filepath(entry, queued_filepath[-1])
100 original_filepath = create_pub_filepath(entry, basename + extension)
101
102 with mgg.public_store.get_file(original_filepath, 'wb') \
103 as original_file:
104 original_file.write(queued_file.read())
105
106 # Remove queued media file from storage and database
107 mgg.queue_store.delete_file(queued_filepath)
108 entry.queued_media_file = []
109
110 # Insert media file information into database
111 media_files_dict = entry.setdefault('media_files', {})
112 media_files_dict['thumb'] = thumb_filepath
113 media_files_dict['original'] = original_filepath
114 media_files_dict['medium'] = medium_filepath
115
116 # Insert exif data into database
117 exif_all = clean_exif(exif_tags)
118
119 if len(exif_all):
120 entry.media_data_init(exif_all=exif_all)
121
122 if len(gps_data):
123 for key in list(gps_data.keys()):
124 gps_data['gps_' + key] = gps_data.pop(key)
125 entry.media_data_init(**gps_data)
126
127 # clean up workbench
128 workbench.destroy_self()
129
130 if __name__ == '__main__':
131 import sys
132 import pprint
133
134 pp = pprint.PrettyPrinter()
135
136 result = extract_exif(sys.argv[1])
137 gps = get_gps_data(result)
138 clean = clean_exif(result)
139 useful = get_useful(clean)
140
141 print pp.pprint(
142 clean)