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
 
@@ -98,24 +98,42 @@ to achieve::
 
                                     output_format="png")
 
  identicon_ascii = generator.generate("john.doe@example.com", 200, 200,
 
                                       output_format="ascii")
 

	
 
  # Identicon can be easily saved to a file.
 
  f = open("sample.png", "wb")
 
  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::
 

	
 
  #!/usr/bin/env python
 

	
 
  # Import the libraries.
 
  import pydenticon
 
  import hashlib
 

	
pydenticon/__init__.py
Show inline comments
 
@@ -209,25 +209,25 @@ class Generator(object):
 
          PIL.ImageColor module.
 

	
 
          background - Colour which should be used for background and padding,
 
          represented as a string of format supported by the PIL.ImageColor
 
          module.
 

	
 
        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
 
        block_height = height // self.rows
 

	
 
        # Go through all the elements of a matrix, and draw the rectangles.
 
        for row, row_columns in enumerate(matrix):
 
            for column, cell in enumerate(row_columns):
 
                if cell:
tests/test_pydenticon.py
Show inline comments
 
@@ -180,25 +180,25 @@ class GeneratorTest(unittest.TestCase):
 

	
 
        # Generate the raw image.
 
        raw_image = generator._generate_png(matrix, width, height, padding, foreground, background)
 

	
 
        # Try to load the raw image.
 
        image_stream = BytesIO(raw_image)
 
        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.
 
        """
 

	
 
        # Set-up parameters that will be used for generating the image.
 
        foreground = "1"
 
        background = "0"
 
        matrix = [
 
            [0, 0, 1, 0, 0],
 
            [0, 0, 1, 0, 0],
 
@@ -350,28 +350,28 @@ class GeneratorTest(unittest.TestCase):
 
                      "rgb(49,203,115)",
 
                      "rgb(141,69,170)"]
 

	
 
        # Set-up a background colour (taken from Sigil). Same as used for
 
        # reference images.
 
        background = "rgb(224,224,224)"
 

	
 
        # 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)
 
        image_stream = BytesIO(raw_image)
 
        test1 = PIL.Image.open(image_stream)
 

	
 
        # Generate second test identicon.
 
        raw_image = generator.generate("test2", width, height, padding=padding)
 
        image_stream = BytesIO(raw_image)
 
@@ -380,20 +380,20 @@ class GeneratorTest(unittest.TestCase):
 
        # Generate third test identicon.
 
        raw_image = generator.generate("test3", width, height, padding=padding)
 
        image_stream = BytesIO(raw_image)
 
        test3 = PIL.Image.open(image_stream)
 

	
 
        # Calculate differences between generated identicons and references.
 
        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__':
 
    unittest.main()
0 comments (0 inline, 0 general)