replaced alpha composite function
This commit is contained in:
parent
16c21d46bf
commit
a92322b794
49
ascii.py
49
ascii.py
|
@ -37,7 +37,7 @@ def scale_image(image, maxDim=100):
|
||||||
new_height = maxDim
|
new_height = maxDim
|
||||||
aspect_ratio = original_width/float(original_height)
|
aspect_ratio = original_width/float(original_height)
|
||||||
new_width = int(aspect_ratio * new_height)
|
new_width = int(aspect_ratio * new_height)
|
||||||
image = image.resize((new_width, new_height), Image.ANTIALIAS)
|
image = image.resize((new_width, new_height))
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
@ -141,46 +141,15 @@ def open_image(imagePath):
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
|
||||||
def alpha_composite(front, back):
|
def alpha_composite(image, color=(255, 255, 255)):
|
||||||
"""Alpha composite two RGBA images.
|
"""
|
||||||
|
Alpha composite an RGBA Image with a specified color.
|
||||||
Source: http://stackoverflow.com/a/9166671/284318
|
Source: http://stackoverflow.com/a/9166671/284318
|
||||||
|
|
||||||
Keyword Arguments:
|
|
||||||
front -- PIL RGBA Image object
|
|
||||||
back -- PIL RGBA Image object
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
front = np.asarray(front)
|
image.load() # needed for split()
|
||||||
back = np.asarray(back)
|
background = Image.new('RGB', image.size, color)
|
||||||
result = np.empty(front.shape, dtype='float')
|
background.paste(image, mask=image.split()[3]) # 3 is the alpha channel
|
||||||
alpha = np.index_exp[:, :, 3:]
|
return background
|
||||||
rgb = np.index_exp[:, :, :3]
|
|
||||||
falpha = front[alpha] / 255.0
|
|
||||||
balpha = back[alpha] / 255.0
|
|
||||||
result[alpha] = falpha + balpha * (1 - falpha)
|
|
||||||
old_setting = np.seterr(invalid='ignore')
|
|
||||||
result[rgb] = (front[rgb] * falpha + back[rgb] * balpha * (1 - falpha)) / result[alpha]
|
|
||||||
np.seterr(**old_setting)
|
|
||||||
result[alpha] *= 255
|
|
||||||
np.clip(result, 0, 255)
|
|
||||||
# astype('uint8') maps np.nan and np.inf to 0
|
|
||||||
result = result.astype('uint8')
|
|
||||||
result = Image.fromarray(result, 'RGBA')
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def alpha_composite_with_color(image, color=(255, 255, 255)):
|
|
||||||
"""Alpha composite an RGBA image with a single color image of the
|
|
||||||
specified color and the same size as the original image.
|
|
||||||
|
|
||||||
Keyword Arguments:
|
|
||||||
image -- PIL RGBA Image object
|
|
||||||
color -- Tuple r, g, b (default 255, 255, 255)
|
|
||||||
|
|
||||||
"""
|
|
||||||
back = Image.new('RGBA', size=image.size, color=color + (255,))
|
|
||||||
return alpha_composite(image, back)
|
|
||||||
|
|
||||||
|
|
||||||
def image_to_ascii(image=None, reverse=False, brail=False, color=None,**kwargs):
|
def image_to_ascii(image=None, reverse=False, brail=False, color=None,**kwargs):
|
||||||
|
@ -195,7 +164,7 @@ def image_to_ascii(image=None, reverse=False, brail=False, color=None,**kwargs):
|
||||||
if image.mode == "P":
|
if image.mode == "P":
|
||||||
image = image.convert(image.palette.mode)
|
image = image.convert(image.palette.mode)
|
||||||
if image.mode == "RGBA":
|
if image.mode == "RGBA":
|
||||||
image = alpha_composite_with_color(image).convert("RGB")
|
image = alpha_composite(image).convert("RGB")
|
||||||
|
|
||||||
image = scale_image(image)
|
image = scale_image(image)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user