From 40945bbc1f23eae00e9f3683558ba24cb4216bb7 Mon Sep 17 00:00:00 2001 From: Shiwank19 Date: Wed, 17 Apr 2019 18:47:40 +0530 Subject: [PATCH 1/2] Implemented mising blending filters --- p5/core/image.py | 95 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 8 deletions(-) diff --git a/p5/core/image.py b/p5/core/image.py index d01f9c4e..81fcc662 100644 --- a/p5/core/image.py +++ b/p5/core/image.py @@ -438,6 +438,7 @@ def blend(self, other, mode): other_img = other._img # todo: implement missing filters -- abhikpal (2018-08-14) + #status :missing filters implemented -- Shiwank (2019-04-17) if mode == 'blend': self._img = ImageChops.composite(self._img, other_img, self._img) elif mode == 'add': @@ -447,25 +448,103 @@ def blend(self, other, mode): elif mode == 'lightest': self._img = ImageChops.lighter(self._img, other_img) elif mode == 'darkest': - self._img = ImageChops.darker(self._img, other_img) + self._img = ImageChops.darker(self._img, other_img) elif mode == 'difference': - raise NotImplementedError + self._img = ImageChops.difference(self._img, other_img) + #for treating alpha=255 images + for i in range(0,self.size[0]): + for j in range(0,self.size[1]): + self_pixel = self._img.getpixel((i,j)) + self._img.putpixel((i,j), (self_pixel[0], self_pixel[1], self_pixel[2], 255)) elif mode == 'exclusion': - raise NotImplementedError + #formula used: + #Target+Blend-2*(Target)*(Blend) ,0<=Target,Blend<=1 + for i in range(0, self.size[0]): + for j in range(0, self.size[1]): + self_pixel = list(self._img.getpixel((i,j))) + other_pixel = list(other_img.getpixel((i,j))) + for k in range(0,len(self_pixel)): + self_pixel[k] = self_pixel[k]/255 + other_pixel[k] = other_pixel[k]/255 + self_pixel[k] = int(((self_pixel[k]) + (other_pixel[k]) - 2 * (self_pixel[k]) * (other_pixel[k])) * 255) + #alternate formula + #self_pixel[k]=int((0.5-2*(0.5-self_pixel[k])*(0.5-other_pixel[k]))*255) + self._img.putpixel((i, j), (self_pixel[0], self_pixel[1], self_pixel[2], 255)) elif mode == 'multiply': self._img = ImageChops.multiply(self._img, other_img) elif mode == 'screen': self._img = ImageChops.screen(self._img, other_img) elif mode == 'overlay': - raise NotImplementedError + #formula used: + #(Target > 0.5)*(1-(1-2*(Target-0.5))*(1-Blend))+(Target <= 0.5) * ((2*Target) * Blend) ,0<=Target,Blend<=1 + + for i in range(0, self.size[0]): + for j in range(0, self.size[1]): + self_pixel = list(self._img.getpixel((i, j))) + other_pixel = list(other_img.getpixel((i, j))) + for k in range(0, len(self_pixel)): + self_pixel[k] = self_pixel[k] / 255 + other_pixel[k] = other_pixel[k] / 255 + self_pixel[k] = int(((self_pixel[k] > 0.5) * (1 - (1-2 * (self_pixel[k] - 0.5)) * (1 - other_pixel[k])) + (self_pixel[k] <= 0.5) * ((2*self_pixel[k]) * other_pixel[k])) * 255) + self._img.putpixel((i, j), (self_pixel[0], self_pixel[1], self_pixel[2], 255)) elif mode == 'hard_light': - raise NotImplementedError + #formula used: + #(Blend > 0.5)*(1-(1-Target)*(1-2*(Blend-0.5)))+(Blend <= 0.5) * (Target * (2*Blend)) ,0<=Target,Blend<=1 + + for i in range(0, self.size[0]): + for j in range(0, self.size[1]): + self_pixel = list(self._img.getpixel((i, j))) + other_pixel = list(other_img.getpixel((i, j))) + for k in range(0, len(self_pixel)): + self_pixel[k] = self_pixel[k] / 255 + other_pixel[k] = other_pixel[k] / 255 + self_pixel[k] = int(((other_pixel[k] > 0.5) * (1 - ( 1 - self_pixel[k]) * (1 - 2 * (other_pixel[k] - 0.5))) + (other_pixel[k] <= 0.5) * (self_pixel[k] * (2 * other_pixel[k]))) * 255) + self._img.putpixel((i, j), (self_pixel[0], self_pixel[1], self_pixel[2], 255)) elif mode == 'soft_light': - raise NotImplementedError + #formula used: + #(Blend > 0.5)*(1-(1-Target)*(1-(Blend-0.5)))+(Blend <= 0.5) * (Target * (Blend+0.5)) ,0<=Target,Blend<=1 + + for i in range(0, self.size[0]): + for j in range(0, self.size[1]): + self_pixel = list(self._img.getpixel((i, j))) + other_pixel = list(other_img.getpixel((i, j))) + for k in range(0, len(self_pixel)): + self_pixel[k] = self_pixel[k] / 255 + other_pixel[k] = other_pixel[k] / 255 + self_pixel[k] = int(((other_pixel[k] > 0.5) * (1 - (1-self_pixel[k]) * (1 - (other_pixel[k]-0.5))) + (other_pixel[k] <= 0.5) * (self_pixel[k] * (other_pixel[k]+0.5))) * 255) + self._img.putpixel((i,j),(self_pixel[0],self_pixel[1],self_pixel[2],255)) elif mode == 'dodge': - raise NotImplementedError + #formula used: + #Target / (1-Blend) , 0<=Blend,target<=1 + + for i in range(0, self.size[0]): + for j in range(0, self.size[1]): + self_pixel=list(self._img.getpixel((i, j))) + other_pixel=list(other_img.getpixel((i, j))) + for k in range(0, len(self_pixel)): + self_pixel[k] = self_pixel[k] / 255 + other_pixel[k] = other_pixel[k] / 255 + if(other_pixel[k] == 1): + self_pixel[k] = 255 + else: + self_pixel[k] = int((self_pixel[k] / (1-other_pixel[k]))*255) + self._img.putpixel((i, j), (self_pixel[0], self_pixel[1], self_pixel[2], 255)) elif mode == 'burn': - raise NotImplementedError + #formula used: + #1 - (1-Target) / Blend , 0<=Blend,target<=1 + + for i in range(0, self.size[0]): + for j in range(0, self.size[1]): + self_pixel = list(self._img.getpixel((i, j))) + other_pixel = list(other_img.getpixel((i, j))) + for k in range(0, len(self_pixel)): + self_pixel[k] = self_pixel[k] / 255 + other_pixel[k] = other_pixel[k] / 255 + if(other_pixel == 0): + self_pixel = 255 + else: + self_pixel[k] = int((1 - (1-self_pixel[k]) / other_pixel[k]) * 255) + self._img.putpixel((i, j), (self_pixel[0], self_pixel[1], self_pixel[2], 255)) else: raise KeyError("'{}' blend mode not found".format(mdoe.upper())) From 4071dd4344d29dddcb87fd0c9502a41d5e1cd6f1 Mon Sep 17 00:00:00 2001 From: Shiwank Agrahari <37938063+Shiwank19@users.noreply.github.com> Date: Sun, 21 Apr 2019 11:40:20 +0530 Subject: [PATCH 2/2] fixed missing index in burn blending mode --- p5/core/image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/p5/core/image.py b/p5/core/image.py index 81fcc662..a9bd174a 100644 --- a/p5/core/image.py +++ b/p5/core/image.py @@ -540,8 +540,8 @@ def blend(self, other, mode): for k in range(0, len(self_pixel)): self_pixel[k] = self_pixel[k] / 255 other_pixel[k] = other_pixel[k] / 255 - if(other_pixel == 0): - self_pixel = 255 + if(other_pixel[k] == 0): + self_pixel[k] = 255 else: self_pixel[k] = int((1 - (1-self_pixel[k]) / other_pixel[k]) * 255) self._img.putpixel((i, j), (self_pixel[0], self_pixel[1], self_pixel[2], 255))