_log = logging.getLogger(__name__)
+PIL_FILTERS = {
+ 'NEAREST': Image.NEAREST,
+ 'BILINEAR': Image.BILINEAR,
+ 'BICUBIC': Image.BICUBIC,
+ 'ANTIALIAS': Image.ANTIALIAS}
+
def resize_image(entry, filename, new_path, exif_tags, workdir, new_size,
size_limits=(0, 0)):
- """Store a resized version of an image and return its pathname.
+ """
+ Store a resized version of an image and return its pathname.
Arguments:
entry -- the entry for the image to resize
except IOError:
raise BadMediaFail()
resized = exif_fix_image_orientation(resized, exif_tags) # Fix orientation
- resized.thumbnail(new_size, Image.ANTIALIAS)
+
+ filter_config = \
+ mgg.global_config['media_type:mediagoblin.media_types.image']\
+ ['resize_filter']
+
+ try:
+ resize_filter = PIL_FILTERS[filter_config.upper()]
+ except KeyError:
+ raise Exception('Filter "{0}" not found, choose one of {1}'.format(
+ unicode(filter_config),
+ u', '.join(PIL_FILTERS.keys())))
+
+ resized.thumbnail(new_size, resize_filter)
# Copy the new file to the conversion subdir, then remotely.
tmp_resized_filename = os.path.join(workdir, new_path[-1])
name, ext = os.path.splitext(kw['media'].filename)
clean_ext = ext[1:].lower() # Strip the . from ext and make lowercase
- _log.debug('name: {0}\next: {1}\nlower_ext: {2}'.format(
- name,
- ext,
- clean_ext))
-
if clean_ext in SUPPORTED_FILETYPES:
_log.info('Found file extension in supported filetypes')
return True
return False
-def process_image(entry):
- """
- Code to process an image
+def process_image(proc_state):
+ """Code to process an image. Will be run by celery.
+
+ A Workbench() represents a local tempory dir. It is automatically
+ cleaned up when this function exits.
"""
- workbench = mgg.workbench_manager.create_workbench()
+ entry = proc_state.entry
+ workbench = proc_state.workbench
+
# Conversions subdirectory to avoid collisions
conversions_subdir = os.path.join(
workbench.dir, 'conversions')
os.mkdir(conversions_subdir)
- queued_filepath = entry.queued_media_file
- queued_filename = workbench.localized_file(
- mgg.queue_store, queued_filepath,
- 'source')
+
+ queued_filename = proc_state.get_queued_filename()
name_builder = FilenameBuilder(queued_filename)
# EXIF extraction
else:
medium_filepath = None
- # we have to re-read because unlike PIL, not everything reads
- # things in string representation :)
- queued_file = file(queued_filename, 'rb')
-
- with queued_file:
- original_filepath = create_pub_filepath(
- entry, name_builder.fill('{basename}{ext}'))
-
- with mgg.public_store.get_file(original_filepath, 'wb') \
- as original_file:
- original_file.write(queued_file.read())
+ # Copy our queued local workbench to its final destination
+ proc_state.copy_original(name_builder.fill('{basename}{ext}'))
# Remove queued media file from storage and database
- mgg.queue_store.delete_file(queued_filepath)
- entry.queued_media_file = []
+ proc_state.delete_queue_file()
# Insert media file information into database
media_files_dict = entry.setdefault('media_files', {})
- media_files_dict['thumb'] = thumb_filepath
- media_files_dict['original'] = original_filepath
+ media_files_dict[u'thumb'] = thumb_filepath
if medium_filepath:
- media_files_dict['medium'] = medium_filepath
+ media_files_dict[u'medium'] = medium_filepath
# Insert exif data into database
exif_all = clean_exif(exif_tags)
gps_data['gps_' + key] = gps_data.pop(key)
entry.media_data_init(**gps_data)
- # clean up workbench
- workbench.destroy_self()
if __name__ == '__main__':
import sys