Don't read full image media into RAM on copying (#419)
[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 import logging
20
21 from mediagoblin import mg_globals as mgg
22 from mediagoblin.processing import BadMediaFail, \
23 create_pub_filepath, FilenameBuilder
24 from mediagoblin.tools.exif import exif_fix_image_orientation, \
25 extract_exif, clean_exif, get_gps_data, get_useful, \
26 exif_image_needs_rotation
27
28 _log = logging.getLogger(__name__)
29
30
31 def resize_image(entry, filename, new_path, exif_tags, workdir, new_size,
32 size_limits=(0, 0)):
33 """
34 Store a resized version of an image and return its pathname.
35
36 Arguments:
37 entry -- the entry for the image to resize
38 filename -- the filename of the original image being resized
39 new_path -- public file path for the new resized image
40 exif_tags -- EXIF data for the original image
41 workdir -- directory path for storing converted image files
42 new_size -- 2-tuple size for the resized image
43 """
44 try:
45 resized = Image.open(filename)
46 except IOError:
47 raise BadMediaFail()
48 resized = exif_fix_image_orientation(resized, exif_tags) # Fix orientation
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
58 SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg']
59
60
61 def sniff_handler(media_file, **kw):
62 if kw.get('media') is not None: # That's a double negative!
63 name, ext = os.path.splitext(kw['media'].filename)
64 clean_ext = ext[1:].lower() # Strip the . from ext and make lowercase
65
66 if clean_ext in SUPPORTED_FILETYPES:
67 _log.info('Found file extension in supported filetypes')
68 return True
69 else:
70 _log.debug('Media present, extension not found in {0}'.format(
71 SUPPORTED_FILETYPES))
72 else:
73 _log.warning('Need additional information (keyword argument \'media\')'
74 ' to be able to handle sniffing')
75
76 return False
77
78
79 def process_image(entry):
80 """
81 Code to process an image
82 """
83 workbench = mgg.workbench_manager.create_workbench()
84 # Conversions subdirectory to avoid collisions
85 conversions_subdir = os.path.join(
86 workbench.dir, 'conversions')
87 os.mkdir(conversions_subdir)
88 queued_filepath = entry.queued_media_file
89 queued_filename = workbench.localized_file(
90 mgg.queue_store, queued_filepath,
91 'source')
92 name_builder = FilenameBuilder(queued_filename)
93
94 # EXIF extraction
95 exif_tags = extract_exif(queued_filename)
96 gps_data = get_gps_data(exif_tags)
97
98 # Always create a small thumbnail
99 thumb_filepath = create_pub_filepath(
100 entry, name_builder.fill('{basename}.thumbnail{ext}'))
101 resize_image(entry, queued_filename, thumb_filepath,
102 exif_tags, conversions_subdir,
103 (mgg.global_config['media:thumb']['max_width'],
104 mgg.global_config['media:thumb']['max_height']))
105
106 # If the size of the original file exceeds the specified size of a `medium`
107 # file, a `.medium.jpg` files is created and later associated with the media
108 # entry.
109 medium = Image.open(queued_filename)
110 if medium.size[0] > mgg.global_config['media:medium']['max_width'] \
111 or medium.size[1] > mgg.global_config['media:medium']['max_height'] \
112 or exif_image_needs_rotation(exif_tags):
113 medium_filepath = create_pub_filepath(
114 entry, name_builder.fill('{basename}.medium{ext}'))
115 resize_image(
116 entry, queued_filename, medium_filepath,
117 exif_tags, conversions_subdir,
118 (mgg.global_config['media:medium']['max_width'],
119 mgg.global_config['media:medium']['max_height']))
120 else:
121 medium_filepath = None
122
123 # Copy our queued local workbench to its final destination
124 original_filepath = create_pub_filepath(
125 entry, name_builder.fill('{basename}{ext}'))
126 mgg.public_store.copy_local_to_storage(queued_filename, original_filepath)
127
128 # Remove queued media file from storage and database
129 mgg.queue_store.delete_file(queued_filepath)
130 entry.queued_media_file = []
131
132 # Insert media file information into database
133 media_files_dict = entry.setdefault('media_files', {})
134 media_files_dict[u'thumb'] = thumb_filepath
135 media_files_dict[u'original'] = original_filepath
136 if medium_filepath:
137 media_files_dict[u'medium'] = medium_filepath
138
139 # Insert exif data into database
140 exif_all = clean_exif(exif_tags)
141
142 if len(exif_all):
143 entry.media_data_init(exif_all=exif_all)
144
145 if len(gps_data):
146 for key in list(gps_data.keys()):
147 gps_data['gps_' + key] = gps_data.pop(key)
148 entry.media_data_init(**gps_data)
149
150 # clean up workbench
151 workbench.destroy_self()
152
153 if __name__ == '__main__':
154 import sys
155 import pprint
156
157 pp = pprint.PrettyPrinter()
158
159 result = extract_exif(sys.argv[1])
160 gps = get_gps_data(result)
161 clean = clean_exif(result)
162 useful = get_useful(clean)
163
164 print pp.pprint(
165 clean)