Removing superfluous whitespace
[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, FilenameBuilder
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 resize_image(entry, filename, new_path, exif_tags, workdir, new_size,
28 size_limits=(0, 0)):
29 """Store a resized version of an image and return its pathname.
30
31 Arguments:
32 entry -- the entry for the image to resize
33 filename -- the filename of the original image being resized
34 new_path -- public file path for the new resized image
35 exif_tags -- EXIF data for the original image
36 workdir -- directory path for storing converted image files
37 new_size -- 2-tuple size for the resized image
38 size_limits (optional) -- image is only resized if it exceeds this size
39
40 """
41 try:
42 resized = Image.open(filename)
43 except IOError:
44 raise BadMediaFail()
45 resized = exif_fix_image_orientation(resized, exif_tags) # Fix orientation
46
47 if ((resized.size[0] > size_limits[0]) or
48 (resized.size[1] > size_limits[1])):
49 resized.thumbnail(new_size, Image.ANTIALIAS)
50
51 # Copy the new file to the conversion subdir, then remotely.
52 tmp_resized_filename = os.path.join(workdir, new_path[-1])
53 with file(tmp_resized_filename, 'w') as resized_file:
54 resized.save(resized_file)
55 mgg.public_store.copy_local_to_storage(tmp_resized_filename, new_path)
56
57 def process_image(entry):
58 """
59 Code to process an image
60 """
61 workbench = mgg.workbench_manager.create_workbench()
62 # Conversions subdirectory to avoid collisions
63 conversions_subdir = os.path.join(
64 workbench.dir, 'conversions')
65 os.mkdir(conversions_subdir)
66 queued_filepath = entry.queued_media_file
67 queued_filename = workbench.localized_file(
68 mgg.queue_store, queued_filepath,
69 'source')
70 name_builder = FilenameBuilder(queued_filename)
71
72 # EXIF extraction
73 exif_tags = extract_exif(queued_filename)
74 gps_data = get_gps_data(exif_tags)
75
76 # Always create a small thumbnail
77 thumb_filepath = create_pub_filepath(
78 entry, name_builder.fill('{basename}.thumbnail{ext}'))
79 resize_image(entry, queued_filename, thumb_filepath,
80 exif_tags, conversions_subdir, THUMB_SIZE)
81
82 # If the size of the original file exceeds the specified size of a `medium`
83 # file, a `.medium.jpg` files is created and later associated with the media
84 # entry.
85 medium = Image.open(queued_filename)
86 if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1] \
87 or exif_image_needs_rotation(exif_tags):
88 medium_filepath = create_pub_filepath(
89 entry, name_builder.fill('{basename}.medium{ext}'))
90 resize_image(
91 entry, queued_filename, medium_filepath,
92 exif_tags, conversions_subdir, MEDIUM_SIZE, MEDIUM_SIZE)
93 else:
94 medium_filepath = None
95
96 # we have to re-read because unlike PIL, not everything reads
97 # things in string representation :)
98 queued_file = file(queued_filename, 'rb')
99
100 with queued_file:
101 original_filepath = create_pub_filepath(
102 entry, name_builder.fill('{basename}{ext}') )
103
104 with mgg.public_store.get_file(original_filepath, 'wb') \
105 as original_file:
106 original_file.write(queued_file.read())
107
108 # Remove queued media file from storage and database
109 mgg.queue_store.delete_file(queued_filepath)
110 entry.queued_media_file = []
111
112 # Insert media file information into database
113 media_files_dict = entry.setdefault('media_files', {})
114 media_files_dict['thumb'] = thumb_filepath
115 media_files_dict['original'] = original_filepath
116 if medium_filepath:
117 media_files_dict['medium'] = medium_filepath
118
119 # Insert exif data into database
120 exif_all = clean_exif(exif_tags)
121
122 if len(exif_all):
123 entry.media_data_init(exif_all=exif_all)
124
125 if len(gps_data):
126 for key in list(gps_data.keys()):
127 gps_data['gps_' + key] = gps_data.pop(key)
128 entry.media_data_init(**gps_data)
129
130 # clean up workbench
131 workbench.destroy_self()
132
133 if __name__ == '__main__':
134 import sys
135 import pprint
136
137 pp = pprint.PrettyPrinter()
138
139 result = extract_exif(sys.argv[1])
140 gps = get_gps_data(result)
141 clean = clean_exif(result)
142 useful = get_useful(clean)
143
144 print pp.pprint(
145 clean)