Changeset - 57a095aafd9b
[Not reviewed]
0 3 0
Branko Majic (branko) - 7 years ago 2016-12-03 13:26:56
branko@majic.rs
PYD-6: Switched to using RGBA in order to support transparency out of the box. Updated usage instructions to include info on how to specify transparency. Updated tests to accomodate to change in colour scheme used in images.
3 files changed with 25 insertions and 7 deletions:
0 comments (0 inline, 0 general)
docs/usage.rst
Show inline comments
 
@@ -104,12 +104,30 @@ to achieve::
 
  f.write(identicon_png)
 
  f.close()
 

	
 
  # ASCII identicon can be printed-out to console directly.
 
  print identicon_ascii
 

	
 

	
 
Working with transparency
 
-------------------------
 

	
 
.. note::
 
   New in version ``0.3``.
 

	
 
If you ever find yourself in need of having a transparent background or
 
foreground, you can easily do this using the syntax
 
``rgba(224,224,224,0)``. All this does is effectively adding alpha channel to
 
selected colour.
 

	
 
The alpha channel value ranges from ``0`` to ``255``, letting you specify how
 
much transparency/opaqueness you want. For example, to have it at roughly 50%
 
(more like at ``50.2%`` since you can't use fractions), you would simply specify
 
value as ``rgba(224,224,224,128)``.
 

	
 

	
 
Full example
 
------------
 

	
 
Finally, here is a full example that will create a number of identicons and
 
output them in PNG format to local directory::
 

	
pydenticon/__init__.py
Show inline comments
 
@@ -215,13 +215,13 @@ class Generator(object):
 
        Returns:
 

	
 
          Identicon image in PNG format, returned as a byte list.
 
        """
 

	
 
        # Set-up a new image object, setting the background to provided value.
 
        image = Image.new("RGB", (width + padding[2] + padding[3], height + padding[0] + padding[1]), background)
 
        image = Image.new("RGBA", (width + padding[2] + padding[3], height + padding[0] + padding[1]), background)
 

	
 
        # Set-up a draw image (for drawing the blocks).
 
        draw = ImageDraw.Draw(image)
 

	
 
        # Calculate the block widht and height.
 
        block_width = width // self.columns
tests/test_pydenticon.py
Show inline comments
 
@@ -186,13 +186,13 @@ class GeneratorTest(unittest.TestCase):
 
        image = PIL.Image.open(image_stream)
 

	
 
        # Verify image size, format, and mode.
 
        self.assertEqual(image.size[0], 240)
 
        self.assertEqual(image.size[1], 240)
 
        self.assertEqual(image.format, "PNG")
 
        self.assertEqual(image.mode, "RGB")
 
        self.assertEqual(image.mode, "RGBA")
 

	
 
    def test_generate_ascii(self):
 
        """
 
        Tests the generated identicon in ASCII format.
 
        """
 

	
 
@@ -356,16 +356,16 @@ class GeneratorTest(unittest.TestCase):
 

	
 
        # Set-up parameters equivalent as used for samples.
 
        width = 200
 
        height = 200
 
        padding = (20, 20, 20, 20)
 

	
 
        # Load the reference images, making sure they're in RGB mode.
 
        test1_ref = PIL.Image.open("tests/samples/test1.png").convert(mode="RGB")
 
        test2_ref = PIL.Image.open("tests/samples/test2.png").convert(mode="RGB")
 
        test3_ref = PIL.Image.open("tests/samples/test3.png").convert(mode="RGB")
 
        # Load the reference images, making sure they're in RGBA mode.
 
        test1_ref = PIL.Image.open("tests/samples/test1.png").convert(mode="RGBA")
 
        test2_ref = PIL.Image.open("tests/samples/test2.png").convert(mode="RGBA")
 
        test3_ref = PIL.Image.open("tests/samples/test3.png").convert(mode="RGBA")
 

	
 
        # Set-up the Generator.
 
        generator = Generator(5, 5, foreground=foreground, background=background)
 

	
 
        # Generate first test identicon.
 
        raw_image = generator.generate("test1", width, height, padding=padding)
 
@@ -386,13 +386,13 @@ class GeneratorTest(unittest.TestCase):
 
        diff1 = PIL.ImageChops.difference(test1, test1_ref)
 
        diff2 = PIL.ImageChops.difference(test2, test2_ref)
 
        diff3 = PIL.ImageChops.difference(test3, test3_ref)
 

	
 
        # Verify that all the diffs are essentially black (i.e. no differences
 
        # between generated identicons and reference samples).
 
        expected_extrema = ((0, 0), (0, 0), (0, 0))
 
        expected_extrema = ((0, 0), (0, 0), (0, 0), (0, 0))
 

	
 
        self.assertEqual(diff1.getextrema(), expected_extrema)
 
        self.assertEqual(diff2.getextrema(), expected_extrema)
 
        self.assertEqual(diff3.getextrema(), expected_extrema)
 

	
 
if __name__ == '__main__':
0 comments (0 inline, 0 general)