image resizing: Refactor some decisions into resize_tool.
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Sun, 21 Apr 2013 12:45:40 +0000 (14:45 +0200)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Sat, 27 Apr 2013 15:19:50 +0000 (17:19 +0200)
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.

mediagoblin/media_types/image/processing.py
mediagoblin/tests/test_submission.py

index 16ffcedd3d9844439393c7069fc9f749026de434..9de51a9499f9d7590e581beac02c8d924204c7fe 100644 (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
-
-    # 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
+    resize_tool(proc_state, True, 'thumb',
+                name_builder.fill('{basename}.thumbnail{ext}'),
+                conversions_subdir, exif_tags)
+
+    # 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}'))
index 5ac47316d6340a3d7ca8726e9fd0d031f8972b5f..162b2d195f09eb762124507b380950ca294b76d1 100644 (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