本文整理匯總了Python中PIL.Image.composite方法的典型用法代碼示例。如果您正苦於以下問題:Python Image.composite方法的具體用法?Python Image.composite怎麽用?Python Image.composite使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PIL.Image
的用法示例。
在下文中一共展示了Image.composite方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_rotated_image_labels
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import composite [as 別名]
def get_rotated_image_labels(client, image, bg, phi):
# https://stackoverflow.com/a/5253554
rot = image.rotate(-phi) # clockwise
image_tf = Image.composite(rot, bg, rot)
filename = str(phi) + '.png'
image_tf.convert('RGB').save(os.path.join(output_path, filename))
# https://stackoverflow.com/a/33117447
imgByteArr = io.BytesIO()
image_tf.save(imgByteArr, format='PNG')
imgByteArr = imgByteArr.getvalue()
image = types.Image(content=imgByteArr)
response = client.label_detection(image=image)
return response
示例2: lomoize
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import composite [as 別名]
def lomoize (image,darkness,saturation):
(width,height) = image.size
max = width
if height > width:
max = height
mask = Image.open("./lomolive/lomomask.jpg").resize((max,max))
left = round((max - width) / 2)
upper = round((max - height) / 2)
mask = mask.crop((left,upper,left+width,upper + height))
# mask = Image.open('mask_l.png')
darker = ImageEnhance.Brightness(image).enhance(darkness)
saturated = ImageEnhance.Color(image).enhance(saturation)
lomoized = Image.composite(saturated,darker,mask)
return lomoized
示例3: mask_img
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import composite [as 別名]
def mask_img(img, attn, upscale=32):
"""
Put attention weights to each region in image.
--------------------
Arguments:
img (ndarray: H x W x C): image data.
attn (ndarray: 14 x 14): attention weights of each region.
upscale (int): the ratio between attention size and image size.
"""
attn = transform.pyramid_expand(attn, upscale=upscale, sigma=20)
attn = misc.toimage(attn).convert("L")
mask = misc.toimage(np.zeros(img.shape, dtype=np.uint8)).convert("RGBA")
img = misc.toimage(img).convert("RGBA")
img = Image.composite(img, mask, attn)
return img
示例4: draw_mask_on_image_array
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import composite [as 別名]
def draw_mask_on_image_array(image, mask, color='red', alpha=0.7):
"""Draws mask on an image.
Args:
image: uint8 numpy array with shape (img_height, img_height, 3)
mask: a float numpy array of shape (img_height, img_height) with
values between 0 and 1
color: color to draw the keypoints with. Default is red.
alpha: transparency value between 0 and 1. (default: 0.7)
Raises:
ValueError: On incorrect data type for image or masks.
"""
if image.dtype != np.uint8:
raise ValueError('`image` not of type np.uint8')
if mask.dtype != np.float32:
raise ValueError('`mask` not of type np.float32')
if np.any(np.logical_or(mask > 1.0, mask < 0.0)):
raise ValueError('`mask` elements should be in [0, 1]')
rgb = ImageColor.getrgb(color)
pil_image = Image.fromarray(image)
solid_color = np.expand_dims(
np.ones_like(mask), axis=2) * np.reshape(list(rgb), [1, 1, 3])
pil_solid_color = Image.fromarray(np.uint8(solid_color)).convert('RGBA')
pil_mask = Image.fromarray(np.uint8(255.0*alpha*mask)).convert('L')
pil_image = Image.composite(pil_solid_color, pil_image, pil_mask)
np.copyto(image, np.array(pil_image.convert('RGB')))
示例5: draw_mask_on_image_array
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import composite [as 別名]
def draw_mask_on_image_array(image, mask, color='red', alpha=0.7):
"""Draws mask on an image.
Args:
image: uint8 numpy array with shape (img_height, img_height, 3)
mask: a uint8 numpy array of shape (img_height, img_height) with
values between either 0 or 1.
color: color to draw the keypoints with. Default is red.
alpha: transparency value between 0 and 1. (default: 0.7)
Raises:
ValueError: On incorrect data type for image or masks.
"""
if image.dtype != np.uint8:
raise ValueError('`image` not of type np.uint8')
if mask.dtype != np.uint8:
raise ValueError('`mask` not of type np.uint8')
if np.any(np.logical_and(mask != 1, mask != 0)):
raise ValueError('`mask` elements should be in [0, 1]')
rgb = ImageColor.getrgb(color)
pil_image = Image.fromarray(image)
solid_color = np.expand_dims(
np.ones_like(mask), axis=2) * np.reshape(list(rgb), [1, 1, 3])
pil_solid_color = Image.fromarray(np.uint8(solid_color)).convert('RGBA')
pil_mask = Image.fromarray(np.uint8(255.0*alpha*mask)).convert('L')
pil_image = Image.composite(pil_solid_color, pil_image, pil_mask)
np.copyto(image, np.array(pil_image.convert('RGB')))
示例6: draw_mask_on_image_array
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import composite [as 別名]
def draw_mask_on_image_array(image, mask, color='red', alpha=0.4):
"""Draws mask on an image.
Args:
image: uint8 numpy array with shape (img_height, img_height, 3)
mask: a uint8 numpy array of shape (img_height, img_height) with
values between either 0 or 1.
color: color to draw the keypoints with. Default is red.
alpha: transparency value between 0 and 1. (default: 0.4)
Raises:
ValueError: On incorrect data type for image or masks.
"""
if image.dtype != np.uint8:
raise ValueError('`image` not of type np.uint8')
if mask.dtype != np.uint8:
raise ValueError('`mask` not of type np.uint8')
if np.any(np.logical_and(mask != 1, mask != 0)):
raise ValueError('`mask` elements should be in [0, 1]')
if image.shape[:2] != mask.shape:
raise ValueError('The image has spatial dimensions %s but the mask has '
'dimensions %s' % (image.shape[:2], mask.shape))
rgb = ImageColor.getrgb(color)
pil_image = Image.fromarray(image)
solid_color = np.expand_dims(
np.ones_like(mask), axis=2) * np.reshape(list(rgb), [1, 1, 3])
pil_solid_color = Image.fromarray(np.uint8(solid_color)).convert('RGBA')
pil_mask = Image.fromarray(np.uint8(255.0*alpha*mask)).convert('L')
pil_image = Image.composite(pil_solid_color, pil_image, pil_mask)
np.copyto(image, np.array(pil_image.convert('RGB')))
示例7: put_transparent_mask
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import composite [as 別名]
def put_transparent_mask(img, mask, palette):
mask_np = np.array(mask, dtype=np.uint8)
mask_png = Image.fromarray(mask_np, mode='P')
mask_png.putpalette(palette)
mask_rgb = mask_png.convert('RGB')
mask_np[mask_np > 0] = 130
mask_np[mask_np == 0] = 255
mask_l = Image.fromarray(mask_np, mode='L')
out_image = Image.composite(img, mask_rgb, mask_l)
return out_image
示例8: mask_image
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import composite [as 別名]
def mask_image(img, mask, opacity=1.00, bg=False):
"""
- img (PIL)
- mask (PIL)
- opacity (float) (default: 1.00)
Returns a PIL image.
"""
blank = Image.new('RGB', img.size, color=0)
if bg:
masked_image = Image.composite(blank, img, mask)
else:
masked_image = Image.composite(img, blank, mask)
if opacity < 1:
masked_image = Image.blend(img, masked_image, opacity)
return masked_image
示例9: draw_mask_on_image_array
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import composite [as 別名]
def draw_mask_on_image_array(image, mask, color='red', alpha=0.7):
"""Draws mask on an image.
Args:
image: uint8 numpy array with shape (img_height, img_height, 3)
mask: a float numpy array of shape (img_height, img_height) with
values between 0 and 1
color: color to draw the keypoints with. Default is red.
alpha: transparency value between 0 and 1. (default: 0.7)
Raises:
ValueError: On incorrect data type for image or masks.
"""
if image.dtype != np.uint8:
raise ValueError('`image` not of type np.uint8')
if mask.dtype != np.float32:
raise ValueError('`mask` not of type np.float32')
if np.any(np.logical_or(mask > 1.0, mask < 0.0)):
raise ValueError('`mask` elements should be in [0, 1]')
rgb = ImageColor.getrgb(color)
pil_image = Image.fromarray(image)
solid_color = np.expand_dims(
np.ones_like(mask), axis=2) * np.reshape(list(rgb), [1, 1, 3])
pil_solid_color = Image.fromarray(np.uint8(solid_color)).convert('RGBA')
pil_mask = Image.fromarray(np.uint8(255.0 * alpha * mask)).convert('L')
pil_image = Image.composite(pil_solid_color, pil_image, pil_mask)
np.copyto(image, np.array(pil_image.convert('RGB')))
開發者ID:PacktPublishing,項目名稱:Hands-On-Machine-Learning-with-OpenCV-4,代碼行數:30,代碼來源:visualization_utils.py