From 57a095aafd9b2ec0c6f20961463898bdf023017c 2016-12-03 13:26:56 From: Branko Majic Date: 2016-12-03 13:26:56 Subject: [PATCH] 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. --- diff --git a/docs/usage.rst b/docs/usage.rst index c8df87e5a2bdcf012cda13ce8fb98bf56b8cf980..92333920bdfa56d5c7022e0de7877f029a24a549 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -107,6 +107,24 @@ to achieve:: # 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 ------------ diff --git a/pydenticon/__init__.py b/pydenticon/__init__.py index 9d484b2d71ede6487104477f7c257096ca9ab51a..4c81ac9ba40c123e5eba64fe39df691514fdb18b 100644 --- a/pydenticon/__init__.py +++ b/pydenticon/__init__.py @@ -218,7 +218,7 @@ class Generator(object): """ # 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) diff --git a/tests/test_pydenticon.py b/tests/test_pydenticon.py index 515b271d5f54c864c7102efd5d138bea5667e67f..d501e299a1b1f6f7a37ff529196da8a92bafcfd2 100644 --- a/tests/test_pydenticon.py +++ b/tests/test_pydenticon.py @@ -189,7 +189,7 @@ class GeneratorTest(unittest.TestCase): 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): """ @@ -359,10 +359,10 @@ class GeneratorTest(unittest.TestCase): 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) @@ -389,7 +389,7 @@ class GeneratorTest(unittest.TestCase): # 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)