+MAX_FILENAME_LENGTH = 255 # the limit in VFAT -- seems like a good baseline
+
+def resize_image(entry, filename, basename, file_tail, exif_tags, workdir,
+ new_size, size_limits=None):
+ """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
+ basename -- simple basename of the given filename
+ file_tail -- ending string and extension for the resized filename
+ 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 ((size_limits is None) or
+ (resized.size[0] > size_limits[0]) or
+ (resized.size[1] > size_limits[1])):
+ resized.thumbnail(new_size, Image.ANTIALIAS)
+
+ resized_filename = (basename[:MAX_FILENAME_LENGTH - len(file_tail)] +
+ file_tail)
+ resized_filepath = create_pub_filepath(entry, resized_filename)
+
+ # Copy the new file to the conversion subdir, then remotely.
+ tmp_resized_filename = os.path.join(workdir, resized_filename)
+ with file(tmp_resized_filename, 'w') as resized_file:
+ resized.save(resized_file)
+ mgg.public_store.copy_local_to_storage(
+ tmp_resized_filename, resized_filepath)
+ return resized_filepath
+