Repository: uccser/cs-unplugged

Issue:
Increase size of pixel painter resource thumbnails
Currently as they are 1:1 size of base image, the thumbnails are too small.


Discussion:

Resolving patch:
diff --git a/csunplugged/resources/generators/PixelPainterResourceGenerator.py b/csunplugged/resources/generators/PixelPainterResourceGenerator.py
--- a/csunplugged/resources/generators/PixelPainterResourceGenerator.py
+++ b/csunplugged/resources/generators/PixelPainterResourceGenerator.py
@@ -4,7 +4,6 @@
 from math import ceil
 from yattag import Doc
 import string
-from shutil import copy2
 from resources.utils.BaseResourceGenerator import BaseResourceGenerator
 from django.utils.translation import ugettext as _
 from resources.utils.resource_parameters import EnumResourceParameter
@@ -112,11 +111,7 @@ def data(self):
         """
         method = self.options["method"].value
         image_name = self.options["image"].value
-
-        if method == "run-length-encoding":
-            image_filename = "{}-black-white.png".format(image_name)
-        else:
-            image_filename = "{}-{}.png".format(image_name, method)
+        image_filename = self.get_image_filename(image_name, method)
         image = Image.open(self.STATIC_PATH.format(image_filename))
         (image_width, image_height) = image.size
 
@@ -450,6 +445,22 @@ def create_run_length_encoding_html(self, page_grid_coords, pages_encoding):
                             line("li", ", ".join(str(number) for number in line_values))
         return doc.getvalue()
 
+    def get_image_filename(self, image_name, method):
+        """Return filename for requested image.
+
+        Args:
+            image_name (str): Name of the image.
+            method (str): Type of image used.
+
+        Returns:
+            Image filename (str).
+        """
+        if method == "run-length-encoding":
+            image_filename = "{}-black-white.png".format(image_name)
+        else:
+            image_filename = "{}-{}.png".format(image_name, method)
+        return image_filename
+
     @property
     def subtitle(self):
         """Return the subtitle string of the resource.
@@ -478,8 +489,11 @@ def save_thumbnail(self, resource_name, path):
         """
         method = self.options["method"].value
         image_name = self.options["image"].value
-        if method == "run-length-encoding":
-            image_filename = "{}-black-white.png".format(image_name)
-        else:
-            image_filename = "{}-{}.png".format(image_name, method)
-        copy2(self.STATIC_PATH.format(image_filename), path)
+        image_filename = self.get_image_filename(image_name, method)
+        image = Image.open(self.STATIC_PATH.format(image_filename))
+        (width, height) = image.size
+        minimum_pixel_width = 400
+        ratio = ceil(minimum_pixel_width / width)
+        enlarged_size = (width * ratio, height * ratio)
+        enlarged_image = image.resize(enlarged_size, resample=Image.NEAREST)
+        enlarged_image.save(path)


Test patch:
diff --git a/csunplugged/tests/resources/generators/assets/pixel-painter/boat-black-white-thumbnail.png b/csunplugged/tests/resources/generators/assets/pixel-painter/boat-black-white-thumbnail.png
new file mode 100644
Binary files /dev/null and b/csunplugged/tests/resources/generators/assets/pixel-painter/boat-black-white-thumbnail.png differ
diff --git a/csunplugged/tests/resources/generators/assets/pixel-painter/boat-black-white.png b/csunplugged/tests/resources/generators/assets/pixel-painter/boat-black-white.png
Binary files a/csunplugged/tests/resources/generators/assets/pixel-painter/boat-black-white.png and b/csunplugged/tests/resources/generators/assets/pixel-painter/boat-black-white.png differ
diff --git a/csunplugged/tests/resources/generators/test_pixel_painter.py b/csunplugged/tests/resources/generators/test_pixel_painter.py
--- a/csunplugged/tests/resources/generators/test_pixel_painter.py
+++ b/csunplugged/tests/resources/generators/test_pixel_painter.py
@@ -52,7 +52,7 @@ def test_pixel_painter_resource_generator_thumbnail(self):
         generator.save_thumbnail("Test", test_output_path)
         compare_result = cmp(
             test_output_path,
-            "tests/resources/generators/assets/pixel-painter/boat-black-white.png",
+            "tests/resources/generators/assets/pixel-painter/boat-black-white-thumbnail.png",
             shallow=False
         )
         self.assertTrue(compare_result)
@@ -65,7 +65,7 @@ def test_pixel_painter_resource_generator_thumbnail_run_length(self):
         generator.save_thumbnail("Test", test_output_path)
         compare_result = cmp(
             test_output_path,
-            "tests/resources/generators/assets/pixel-painter/boat-black-white.png",
+            "tests/resources/generators/assets/pixel-painter/boat-black-white-thumbnail.png",
             shallow=False
         )
         self.assertTrue(compare_result)