def resize_tool(entry, force, keyname, orig_file, target_name, conversions_subdir, exif_tags, quality, filter, new_size=None): # Use the default size if new_size was not given if not new_size: max_width = mgg.global_config['media:' + keyname]['max_width'] max_height = mgg.global_config['media:' + keyname]['max_height'] new_size = (max_width, max_height) # If the size of the original file exceeds the specified size for the desized # file, a target_name file is created and later associated with the media # entry. # Also created if the file needs rotation, or if forced. try: im = Image.open(orig_file) except IOError: raise BadMediaFail() if force \ or im.size[0] > new_size[0]\ or im.size[1] > new_size[1]\ or exif_image_needs_rotation(exif_tags): resize_image( entry, im, unicode(keyname), target_name, tuple(new_size), exif_tags, conversions_subdir, quality, filter)
def resize_tool(entry, force, keyname, orig_file, target_name, conversions_subdir, exif_tags, quality, filter, new_size=None): # Use the default size if new_size was not given if not new_size: max_width = mgg.global_config['media:' + keyname]['max_width'] max_height = mgg.global_config['media:' + keyname]['max_height'] new_size = (max_width, max_height) # If thumb or medium is already the same quality and size, then don't # reprocess if _skip_resizing(entry, keyname, new_size, quality, filter): _log.info('{0} of same size and quality already in use, skipping ' 'resizing of media {1}.'.format(keyname, entry.id)) return # If the size of the original file exceeds the specified size for the desized # file, a target_name file is created and later associated with the media # entry. # Also created if the file needs rotation, or if forced. try: im = Image.open(orig_file) except IOError: raise BadMediaFail() if force \ or im.size[0] > new_size[0]\ or im.size[1] > new_size[1]\ or exif_image_needs_rotation(exif_tags): resize_image( entry, im, six.text_type(keyname), target_name, tuple(new_size), exif_tags, conversions_subdir, quality, filter)
def resize_tool(entry, force, keyname, orig_file, target_name, conversions_subdir, exif_tags, quality, filter, new_size=None): # Use the default size if new_size was not given if not new_size: max_width = mgg.global_config['media:' + keyname]['max_width'] max_height = mgg.global_config['media:' + keyname]['max_height'] new_size = (max_width, max_height) # If the size of the original file exceeds the specified size for the desized # file, a target_name file is created and later associated with the media # entry. # Also created if the file needs rotation, or if forced. try: im = Image.open(orig_file) except IOError: raise BadMediaFail() if force \ or im.size[0] > new_size[0]\ or im.size[1] > new_size[1]\ or exif_image_needs_rotation(exif_tags): resize_image(entry, im, unicode(keyname), target_name, tuple(new_size), exif_tags, conversions_subdir, quality, filter)
def resize_tool(proc_state, force, keyname, target_name, conversions_subdir, exif_tags): # filename -- the filename of the original image being resized filename = proc_state.get_queued_filename() max_width = mgg.global_config['media:' + keyname]['max_width'] max_height = mgg.global_config['media:' + keyname]['max_height'] # If the size of the original file exceeds the specified size for the desized # file, a target_name file is created and later associated with the media # entry. # Also created if the file needs rotation, or if forced. try: im = Image.open(filename) except IOError: raise BadMediaFail() if force \ or im.size[0] > max_width \ or im.size[1] > max_height \ or exif_image_needs_rotation(exif_tags): resize_image(proc_state, im, unicode(keyname), target_name, (max_width, max_height), exif_tags, conversions_subdir)
def resize_tool(proc_state, force, keyname, target_name, conversions_subdir, exif_tags): # filename -- the filename of the original image being resized filename = proc_state.get_queued_filename() max_width = mgg.global_config['media:' + keyname]['max_width'] max_height = mgg.global_config['media:' + keyname]['max_height'] # If the size of the original file exceeds the specified size for the desized # file, a target_name file is created and later associated with the media # entry. # Also created if the file needs rotation, or if forced. try: im = Image.open(filename) except IOError: raise BadMediaFail() if force \ or im.size[0] > max_width \ or im.size[1] > max_height \ or exif_image_needs_rotation(exif_tags): resize_image( proc_state, im, unicode(keyname), target_name, (max_width, max_height), exif_tags, conversions_subdir)
def resize_tool(entry, force, keyname, orig_file, target_name, conversions_subdir, exif_tags, quality, filter, new_size=None): # Use the default size if new_size was not given if not new_size: max_width = mgg.global_config['media:' + keyname]['max_width'] max_height = mgg.global_config['media:' + keyname]['max_height'] new_size = (max_width, max_height) # If thumb or medium is already the same quality and size, then don't # reprocess if _skip_resizing(entry, keyname, new_size, quality, filter): _log.info('{0} of same size and quality already in use, skipping ' 'resizing of media {1}.'.format(keyname, entry.id)) return # If the size of the original file exceeds the specified size for the desized # file, a target_name file is created and later associated with the media # entry. # Also created if the file needs rotation, or if forced. try: im = Image.open(orig_file) except IOError: raise BadMediaFail() if force \ or im.size[0] > new_size[0]\ or im.size[1] > new_size[1]\ or exif_image_needs_rotation(exif_tags): resize_image(entry, im, six.text_type(keyname), target_name, tuple(new_size), exif_tags, conversions_subdir, quality, filter)
def process_image(entry): """ Code to process an image """ workbench = mgg.workbench_manager.create_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') name_builder = FilenameBuilder(queued_filename) # EXIF extraction exif_tags = extract_exif(queued_filename) gps_data = get_gps_data(exif_tags) # Always create a small thumbnail thumb_filepath = create_pub_filepath( entry, name_builder.fill('{basename}.thumbnail{ext}')) resize_image(entry, queued_filename, thumb_filepath, exif_tags, conversions_subdir, (mgg.global_config['media:thumb']['max_width'], mgg.global_config['media:thumb']['max_height'])) # If the size of the original file exceeds the specified size of a `medium` # file, a `.medium.jpg` files is created and later associated with the media # entry. medium = Image.open(queued_filename) if medium.size[0] > mgg.global_config['media:medium']['max_width'] \ or medium.size[1] > mgg.global_config['media:medium']['max_height'] \ or exif_image_needs_rotation(exif_tags): medium_filepath = create_pub_filepath( entry, name_builder.fill('{basename}.medium{ext}')) resize_image( entry, queued_filename, medium_filepath, exif_tags, conversions_subdir, (mgg.global_config['media:medium']['max_width'], mgg.global_config['media:medium']['max_height'])) 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()) # Remove queued media file from storage and database mgg.queue_store.delete_file(queued_filepath) entry.queued_media_file = [] # Insert media file information into database media_files_dict = entry.setdefault('media_files', {}) media_files_dict[u'thumb'] = thumb_filepath media_files_dict[u'original'] = original_filepath if medium_filepath: media_files_dict[u'medium'] = medium_filepath # Insert exif data into database exif_all = clean_exif(exif_tags) if len(exif_all): entry.media_data_init(exif_all=exif_all) if len(gps_data): for key in list(gps_data.keys()): gps_data['gps_' + key] = gps_data.pop(key) entry.media_data_init(**gps_data) # clean up workbench workbench.destroy_self()