Преглед на файлове

Handle subdirectories in thumbnailer.

Patrick Cloke преди 9 години
родител
ревизия
5a133f1b28
променени са 4 файла, в които са добавени 44 реда и са изтрити 17 реда
  1. 5 5
      thumbnailer/Readme.md
  2. BIN
      thumbnailer/test_data/subdir/sample_image.jpg
  3. 27 5
      thumbnailer/test_thumbnails.py
  4. 12 7
      thumbnailer/thumbnailer.py

+ 5 - 5
thumbnailer/Readme.md

@@ -1,14 +1,14 @@
 Thumbnail Creation of images
 ============================
 
-This plugin creates thumbnails for all of the images found under a specific directory, in various thumbnail sizes
+This plugin creates thumbnails for all of the images found under a specific directory, in various thumbnail sizes.
 It requires PIL to function properly since PIL is used to resize the images, and will only rebuild a thumbnail if it
-doesn't already exists (to save processing time)
+doesn't already exists (to save processing time).
 
 Installation
 -------------
 
-Setup up like a normal plugin by setting PLUGIN_PATH, and adding "thumbnailer" to the PLUGINS list
+Setup up like a normal plugin by setting PLUGIN_PATHS, and adding "thumbnailer" to the PLUGINS list
 
 Configuration
 -------------
@@ -17,11 +17,11 @@ Configuration
 * THUMBNAIL_DIR is the path to the output sub directory where the thumbnails are generated
 * THUMBNAIL_SIZES is a dictionary mapping name of size to size specifications.
   The generated filename will be originalname_thumbnailname.ext unless THUMBNAIL_KEEP_NAME is set.
-* THUMBNAIL_KEEP_NAME is a boolean which if set puts the file with the original name in a thumbnailname folder.
+* THUMBNAIL_KEEP_NAME is a boolean which if set puts the thumbnails (with their original filename) in a folder named like the key in THUMBNAIL_SIZES
 
 Sizes can be specified using any of the following formats:
 
 * wxh will resize to exactly wxh cropping as necessary to get that size
 * wx? will resize so that the width is the specified size, and the height will scale to retain aspect ratio
 * ?xh same as wx? but will height being a set size
-* s is a shorthand for wxh where w=h
+* s is a shorthand for wxh where w=h

BIN
thumbnailer/test_data/subdir/sample_image.jpg


+ 27 - 5
thumbnailer/test_thumbnails.py

@@ -13,24 +13,46 @@ class ThumbnailerTests(TestCase):
         self.img = Image.open(self.path("sample_image.jpg"))
 
     def testSquare(self):
-        r = _resizer('square', '100')
+        r = _resizer('square', '100', self.img_path)
         output = r.resize(self.img)
         self.assertEqual((100, 100), output.size)
 
     def testExact(self):
-        r = _resizer('exact', '250x100')
+        r = _resizer('exact', '250x100', self.img_path)
         output = r.resize(self.img)
         self.assertEqual((250, 100), output.size)
 
     def testWidth(self):
-        r = _resizer('aspect', '250x?')
+        r = _resizer('aspect', '250x?', self.img_path)
         output = r.resize(self.img)
         self.assertEqual((250, 166), output.size)
 
     def testHeight(self):
-        r = _resizer('aspect', '?x250')
+        r = _resizer('aspect', '?x250', self.img_path)
         output = r.resize(self.img)
         self.assertEqual((375, 250), output.size)
 
+class ThumbnailerFilenameTest(TestCase):
+
+    def path(self, *parts):
+        return path.join(self.img_path, *parts)
+
+    def setUp(self):
+        self.img_path = path.join(path.dirname(__file__), "test_data")
+
+    def testRoot(self):
+        """Test a file that is in the root of img_path."""
+
+        r = _resizer('square', '100', self.img_path)
+        new_name = r.get_thumbnail_name(self.path('sample_image.jpg'))
+        self.assertEqual('sample_image_square.jpg', new_name)
+
+    def testSubdir(self):
+        """Test a file that is in a sub-directory of img_path."""
+
+        r = _resizer('square', '100', self.img_path)
+        new_name = r.get_thumbnail_name(self.path('subdir', 'sample_image.jpg'))
+        self.assertEqual('subdir/sample_image_square.jpg', new_name)
+
 if __name__=="__main__":
-    main()
+    main()

+ 12 - 7
thumbnailer/thumbnailer.py

@@ -28,9 +28,11 @@ class _resizer(object):
 
     REGEX = re.compile(r'(\d+|\?)x(\d+|\?)')
 
-    def __init__(self, name, spec):
+    def __init__(self, name, spec, root):
         self._name = name
         self._spec = spec
+        # The location of input images from _image_path.
+        self._root = root
 
     def _null_resize(self, w, h, image):
         return image
@@ -86,16 +88,18 @@ class _resizer(object):
         return resizer(targetw, targeth, image)
 
     def get_thumbnail_name(self, in_path):
-        new_filename = path.basename(in_path)
+        # Find the partial path + filename beyond the input image directory.
+        prefix = path.commonprefix([in_path, self._root])
+        new_filename = in_path[len(prefix) + 1:]
+
+        # Generate the new filename.
         (basename, ext) = path.splitext(new_filename)
-        basename = "{0}_{1}".format(basename, self._name)
-        new_filename = "{0}{1}".format(basename, ext)
-        return new_filename
+        return "{0}_{1}{2}".format(basename, self._name, ext)
 
     def resize_file_to(self, in_path, out_path, keep_filename=False):
         """ Given a filename, resize and save the image per the specification into out_path
 
-        :param in_path: path to image file to save.  Must be supposed by PIL
+        :param in_path: path to image file to save.  Must be supported by PIL
         :param out_path: path to the directory root for the outputted thumbnails to be stored
         :return: None
         """
@@ -103,6 +107,7 @@ class _resizer(object):
             filename = path.join(out_path, path.basename(in_path))
         else:
             filename = path.join(out_path, self.get_thumbnail_name(in_path))
+        out_path = path.dirname(filename)
         if not path.exists(out_path):
             os.makedirs(out_path)
         if not path.exists(filename):
@@ -130,7 +135,7 @@ def resize_thumbnails(pelican):
                          pelican.settings.get('THUMBNAIL_DIR', DEFAULT_THUMBNAIL_DIR))
 
     sizes = pelican.settings.get('THUMBNAIL_SIZES', DEFAULT_THUMBNAIL_SIZES)
-    resizers = dict((k, _resizer(k, v)) for k,v in sizes.items())
+    resizers = dict((k, _resizer(k, v, in_path)) for k,v in sizes.items())
     logger.debug("Thumbnailer Started")
     for dirpath, _, filenames in os.walk(in_path):
         for filename in filenames: