image resizing: Refactor some decisions into resize_tool.

Loading the thumb/medium sizes from the config, saving
things to the db, and loading the image is now all done by
resize_tool. It still calls resize_image for the actual
work.
This commit is contained in:
Elrond 2013-04-21 14:45:40 +02:00
parent f96c284e0a
commit 3b359dddc7
2 changed files with 38 additions and 29 deletions

View File

@ -37,13 +37,14 @@ PIL_FILTERS = {
'ANTIALIAS': Image.ANTIALIAS}
def resize_image(proc_state, filename, new_path, exif_tags, workdir, new_size):
def resize_image(proc_state, resized, new_path, new_size,
exif_tags, workdir):
"""
Store a resized version of an image and return its pathname.
Arguments:
proc_state -- the processing state for the image to resize
filename -- the filename of the original image being resized
resized -- an image from Image.open() 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
@ -51,10 +52,6 @@ def resize_image(proc_state, filename, new_path, exif_tags, workdir, new_size):
"""
config = mgg.global_config['media_type:mediagoblin.media_types.image']
try:
resized = Image.open(filename)
except IOError:
raise BadMediaFail()
resized = exif_fix_image_orientation(resized, exif_tags) # Fix orientation
filter_config = config['resize_filter']
@ -74,6 +71,33 @@ def resize_image(proc_state, filename, new_path, exif_tags, workdir, new_size):
mgg.public_store.copy_local_to_storage(tmp_resized_filename, new_path)
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()
entry = proc_state.entry
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):
filepath = create_pub_filepath(entry, target_name)
resize_image(
proc_state, im, filepath,
(max_width, max_height),
exif_tags, conversions_subdir)
proc_state.entry.media_files[keyname] = filepath
SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg']
@ -117,29 +141,14 @@ def process_image(proc_state):
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(proc_state, queued_filename, thumb_filepath,
exif_tags, conversions_subdir,
(mgg.global_config['media:thumb']['max_width'],
mgg.global_config['media:thumb']['max_height']))
entry.media_files[u'thumb'] = thumb_filepath
resize_tool(proc_state, True, 'thumb',
name_builder.fill('{basename}.thumbnail{ext}'),
conversions_subdir, exif_tags)
# 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(
proc_state, queued_filename, medium_filepath,
exif_tags, conversions_subdir,
(mgg.global_config['media:medium']['max_width'],
mgg.global_config['media:medium']['max_height']))
entry.media_files[u'medium'] = medium_filepath
# Possibly create a medium
resize_tool(proc_state, False, 'medium',
name_builder.fill('{basename}.medium{ext}'),
conversions_subdir, exif_tags)
# Copy our queued local workbench to its final destination
proc_state.copy_original(name_builder.fill('{basename}{ext}'))

View File

@ -286,7 +286,7 @@ class TestSubmission:
# Does the processed image have a good filename?
filename = os.path.join(
public_store_dir,
*media.media_files.get(key, []))
*media.media_files[key])
assert filename.endswith('_' + basename)
# Is it smaller than the last processed image we looked at?
size = os.stat(filename).st_size