From 705140cc2cd0da4724f8792d8400be6ab4a39a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag=20W=C3=A4stberg?= Date: Fri, 22 Nov 2019 14:03:59 +0100 Subject: [PATCH] add hardlight and softlight chops --- Tests/test_imagechops.py | 15 +-------------- src/PIL/ImageChops.py | 21 +++++++++++++++++++++ src/libImaging/Chops.c | 18 +++++++++--------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/Tests/test_imagechops.py b/Tests/test_imagechops.py index cfdf9365d..f22c9b058 100644 --- a/Tests/test_imagechops.py +++ b/Tests/test_imagechops.py @@ -363,7 +363,7 @@ def test_subtract_modulo_no_clip(): new = ImageChops.subtract_modulo(im1, im2) # Assert - assert new.getpixel((50, 50)) == (241, 167, 127) + self.assertEqual(new.getpixel((50, 50)), (241, 167, 127)) def test_softlight(self): @@ -392,19 +392,6 @@ def test_hardlight(self): self.assertEqual(new.getpixel((15, 100)), (1, 1, 2)) -def test_overlay(self): - # Arrange - im1 = Image.open("Tests/images/hopper.png") - im2 = Image.open("Tests/images/hopper-XYZ.png") - - # Act - new = ImageChops.overlay(im1, im2) - - # Assert - self.assertEqual(new.getpixel((64, 64)), (159, 50, 27)) - self.assertEqual(new.getpixel((15, 100)), (1, 1, 2)) - - def test_logical(): def table(op, a, b): out = [] diff --git a/src/PIL/ImageChops.py b/src/PIL/ImageChops.py index 1dc456156..01e560d76 100644 --- a/src/PIL/ImageChops.py +++ b/src/PIL/ImageChops.py @@ -138,6 +138,27 @@ def screen(image1, image2): image2.load() return image1._new(image1.im.chop_screen(image2.im)) +def softlight(image1, image2): + """ + Superimposes two images on top of each other using the Soft Light algorithm + + :rtype: :py:class:`~PIL.Image.Image` + """ + + image1.load() + image2.load() + return image1._new(image1.im.chop_softlight(image2.im)) + +def hardlight(image1, image2): + """ + Superimposes two images on top of each other using the Hard Light algorithm + + :rtype: :py:class:`~PIL.Image.Image` + """ + + image1.load() + image2.load() + return image1._new(image1.im.chop_hardlight(image2.im)) def softlight(image1, image2): """ diff --git a/src/libImaging/Chops.c b/src/libImaging/Chops.c index cbd65b196..6a9bf0069 100644 --- a/src/libImaging/Chops.c +++ b/src/libImaging/Chops.c @@ -148,27 +148,27 @@ ImagingChopSubtractModulo(Imaging imIn1, Imaging imIn2) } Imaging -ImagingChopSoftLight(Imaging imIn1, Imaging imIn2) +ImagingChopSoftLight(Imaging imIn1, Imaging imIn2) { // CHOP2( ( ( (255-in1[x]) * (in1[x]*in2[x]) ) / 65536) + // ((in1[x] * (255 - ((255 - in1[1]) * (255 - in2[x]) / 255 ) )) / 255), NULL ); - CHOP2( (((255-in1[x]) * (in1[x]*in2[x]) ) / 65536) + + CHOP2( (((255-in1[x]) * (in1[x]*in2[x]) ) / 65536) + (in1[x] * ( 255 - ( (255 - in1[x]) * (255 - in2[x] ) / 255) )) / 255 , NULL ); } Imaging -ImagingChopHardLight(Imaging imIn1, Imaging imIn2) +ImagingChopHardLight(Imaging imIn1, Imaging imIn2) { - CHOP2( (in2[x]<128) ? ( (in1[x]*in2[x])/127) - : 255 - ( ((255-in2[x]) * (255-in1[x])) / 127) + CHOP2( (in2[x]<128) ? ( (in1[x]*in2[x])/127) + : 255 - ( ((255-in2[x]) * (255-in1[x])) / 127) , NULL); } Imaging -ImagingOverlay(Imaging imIn1, Imaging imIn2) +ImagingOverlay(Imaging imIn1, Imaging imIn2) { - CHOP2( (in1[x]<128) ? ( (in1[x]*in2[x])/127) - : 255 - ( ((255-in1[x]) * (255-in2[x])) / 127) + CHOP2( (in1[x]<128) ? ( (in1[x]*in2[x])/127) + : 255 - ( ((255-in1[x]) * (255-in2[x])) / 127) , NULL); -} \ No newline at end of file +}