From: Christopher Allan Webber Date: Mon, 26 Mar 2012 20:09:35 +0000 (-0500) Subject: Merge remote-tracking branch 'remotes/slikdigit/bassam' X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c0516158896e2bdf1592f1331b9ecf1979d4c099;p=mediagoblin.git Merge remote-tracking branch 'remotes/slikdigit/bassam' Conflicts: mediagoblin/media_types/image/processing.py --- c0516158896e2bdf1592f1331b9ecf1979d4c099 diff --cc mediagoblin/media_types/image/processing.py index c484117b,2728ecf2..6e3b972b --- a/mediagoblin/media_types/image/processing.py +++ b/mediagoblin/media_types/image/processing.py @@@ -19,40 -19,11 +19,41 @@@ import o from mediagoblin import mg_globals as mgg from mediagoblin.processing import BadMediaFail, \ - create_pub_filepath, THUMB_SIZE, MEDIUM_SIZE + create_pub_filepath, THUMB_SIZE, MEDIUM_SIZE, FilenameBuilder from mediagoblin.tools.exif import exif_fix_image_orientation, \ - extract_exif, clean_exif, get_gps_data, get_useful + extract_exif, clean_exif, get_gps_data, get_useful, \ + exif_image_needs_rotation +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. + + Arguments: + entry -- the entry for the image to resize + filename -- the filename of the original image being resized + new_path -- public file path for the new resized image + exif_tags -- EXIF data for the original image + workdir -- directory path for storing converted image files + new_size -- 2-tuple size for the resized image + size_limits (optional) -- image is only resized if it exceeds this size + + """ + try: + resized = Image.open(filename) + except IOError: + raise BadMediaFail() + resized = exif_fix_image_orientation(resized, exif_tags) # Fix orientation + + if ((resized.size[0] > size_limits[0]) or + (resized.size[1] > size_limits[1])): + resized.thumbnail(new_size, Image.ANTIALIAS) + + # Copy the new file to the conversion subdir, then remotely. + tmp_resized_filename = os.path.join(workdir, new_path[-1]) + with file(tmp_resized_filename, 'w') as resized_file: + resized.save(resized_file) + mgg.public_store.copy_local_to_storage(tmp_resized_filename, new_path) + def process_image(entry): """ Code to process an image @@@ -73,19 -46,57 +73,25 @@@ exif_tags = extract_exif(queued_filename) gps_data = get_gps_data(exif_tags) - try: - thumb = Image.open(queued_filename) - except IOError: - raise BadMediaFail() - - thumb = exif_fix_image_orientation(thumb, exif_tags) - - thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS) - - # Copy the thumb to the conversion subdir, then remotely. - thumb_filename = 'thumbnail' + extension - thumb_filepath = create_pub_filepath(entry, thumb_filename) - - tmp_thumb_filename = os.path.join( - conversions_subdir, thumb_filename) - - with file(tmp_thumb_filename, 'w') as thumb_file: - thumb.save(thumb_file) - - mgg.public_store.copy_local_to_storage( - tmp_thumb_filename, thumb_filepath) + # 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, THUMB_SIZE) # 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 + # file, a `.medium.jpg` files is created and later associated with the media # entry. - medium_filepath = create_pub_filepath( - entry, name_builder.fill('{basename}.medium{ext}')) - resize_image(entry, queued_filename, medium_filepath, - exif_tags, conversions_subdir, MEDIUM_SIZE, MEDIUM_SIZE) + medium = Image.open(queued_filename) + if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1] \ - or exif_image_needs_rotation(exif_tags): - - medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS) - - - - # Fix orientation - medium = exif_fix_image_orientation(medium, exif_tags) - - - - medium_filename = 'medium' + extension - medium_filepath = create_pub_filepath(entry, medium_filename) - - tmp_medium_filename = os.path.join( - conversions_subdir, medium_filename) - - with file(tmp_medium_filename, 'w') as medium_file: - medium.save(medium_file) - - mgg.public_store.copy_local_to_storage( - tmp_medium_filename, medium_filepath) ++ 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, MEDIUM_SIZE, MEDIUM_SIZE) + else: + medium_filepath = None # we have to re-read because unlike PIL, not everything reads # things in string representation :) @@@ -107,18 -118,16 +113,19 @@@ media_files_dict = entry.setdefault('media_files', {}) media_files_dict['thumb'] = thumb_filepath media_files_dict['original'] = original_filepath - media_files_dict['medium'] = medium_filepath + if medium_filepath: + media_files_dict['medium'] = medium_filepath # Insert exif data into database - media_data = entry.setdefault('media_data', {}) - media_data['exif'] = { - 'clean': clean_exif(exif_tags)} - media_data['exif']['useful'] = get_useful( - media_data['exif']['clean']) - media_data['gps'] = gps_data + 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()