def hue(im1, im2): """Creates a color with the hue of the source color and the saturation and luminosity of the backdrop color. The hue formula is defined as: B(Cb, Cs) = SetLum(SetSat(Cs, Sat(Cb)), Lum(Cb)) See the W3C document: https://www.w3.org/TR/compositing-1/#blendinghue Arguments: im1: A backdrop image. im2: A source image. Returns: The output image. """ r1, g1, b1 = im1.split() # Cb r2, g2, b2 = im2.split() # Cs lum_cb = lum_im(im1) # Lum(Cb) bands = ImageMath.eval('f((r1, g1, b1), (r2, g2, b2), lum_cb)', f=_hue, r1=r1, g1=g1, b1=b1, r2=r2, g2=g2, b2=b2, lum_cb=lum_cb) bands = [_convert(band, 'L').im for band in bands] return Image.merge('RGB', bands)
def _color_dodge_image_math(cb, cs_inv): """Returns ImageMath operands for color dodge blend mode""" cb = _float(cb) cs_inv = _float(cs_inv) cm = ((cb != 0) * (cs_inv == 0) + (cb / cs_inv)) * 255 return _convert(cm, 'L')
def test_set_lum(): im1 = util.fill((1, 1), [0, 128, 255]) im2 = util.fill((1, 1), [128, 128, 128]) r1, g1, b1 = im1.split() r2, g2, b2 = im2.split() c1 = '(float(r1), float(g1), float(b1))' c2 = '(float(r2), float(g2), float(b2))' bands = ImageMath.eval('set_lum({}, lum({}))'.format(c1, c2), set_lum=set_lum, lum=lum, r1=r1, g1=g1, b1=b1, r2=r2, b2=b2, g2=g2) expected1 = [ [pytest.approx(41.13881001122631, 1e-6)], [pytest.approx(148.48874067225782, 1e-6)], [255], ] assert [list(band.im.getdata()) for band in bands] == expected1 im_set_lum = Image.merge('RGB', [_convert(band, 'L').im for band in bands]) expected2 = [(floor(41.13881001122631), floor(148.48874067225782), 255)] assert list(im_set_lum.getdata()) == expected2
def _soft_light(cb, cs, d_cb): """Returns ImageMath operands for soft light""" cb = _float(cb) / 255 cs = _float(cs) / 255 d_cb = _float(d_cb) / 255 c1 = (cs <= .5) * (cb - (1 - 2 * cs) * cb * (1 - cb)) c2 = (cs > .5) * (cb + (2 * cs - 1) * d_cb) return _convert((c1 + c2) * 255, 'L')
def _color(im1, im2): """The color blend mode. Arguments: im1: A backdrop image (RGB). im2: A source image (RGB). Returns: The output image. """ r, g, b = im2.split() # Cs lum_cb = lum_im(im1) # Lum(Cb) lum_cs = lum_im(im2) # Lum(C) in SetLum bands = ImageMath.eval( 'f((r, g, b), lum_cb, lum_cs)', f=_color_image_math, r=r, g=g, b=b, lum_cb=lum_cb, lum_cs=lum_cs) bands = [_convert(band, 'L').im for band in bands] return Image.merge('RGB', bands)
def _hue(im1, im2): """The hue blend mode. Arguments: im1: A backdrop image (RGB). im2: A source image (RGB). Returns: The output image. """ r1, g1, b1 = im1.split() # Cb r2, g2, b2 = im2.split() # Cs lum_cb = lum_im(im1) # Lum(Cb) bands = ImageMath.eval( 'f((r1, g1, b1), (r2, g2, b2), lum_cb)', f=_hue_image_math, r1=r1, g1=g1, b1=b1, r2=r2, g2=g2, b2=b2, lum_cb=lum_cb) bands = [_convert(band, 'L').im for band in bands] return Image.merge('RGB', bands)
def _color_dodge(cb, cs_inv): cb = _float(cb) cs_inv = _float(cs_inv) cm = ((cb != 0) * (cs_inv == 0) + (cb / cs_inv)) * 255 return _convert(cm, 'L')
def _color_burn(cb, cs): cm = (cb == 255) * 255 + \ (cb < 255) * (cs > 0) * (255 - ((255 - cb) * 255 / cs)) return _convert(cm, 'L')
def _color_burn_image_math(cb, cs): """Returns ImageMath operands for color burn blend mode""" cm = (cb == 255) * 255 + \ (cb < 255) * (cs > 0) * (255 - ((255 - cb) * 255 / cs)) return _convert(cm, 'L')