本文整理匯總了Python中PIL.Image.alpha_composite方法的典型用法代碼示例。如果您正苦於以下問題:Python Image.alpha_composite方法的具體用法?Python Image.alpha_composite怎麽用?Python Image.alpha_composite使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PIL.Image
的用法示例。
在下文中一共展示了Image.alpha_composite方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: apply_colormap_on_image
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def apply_colormap_on_image(org_im, activation, colormap_name):
"""
Apply heatmap on image
Args:
org_img (PIL img): Original image
activation_map (numpy arr): Activation map (grayscale) 0-255
colormap_name (str): Name of the colormap
"""
# Get colormap
color_map = mpl_color_map.get_cmap(colormap_name)
no_trans_heatmap = color_map(activation)
# Change alpha channel in colormap to make sure original image is displayed
heatmap = copy.copy(no_trans_heatmap)
heatmap[:, :, 3] = 0.4
heatmap = Image.fromarray((heatmap*255).astype(np.uint8))
no_trans_heatmap = Image.fromarray((no_trans_heatmap*255).astype(np.uint8))
# Apply heatmap on image
heatmap_on_image = Image.new("RGBA", org_im.size)
heatmap_on_image = Image.alpha_composite(heatmap_on_image, org_im.convert('RGBA'))
heatmap_on_image = Image.alpha_composite(heatmap_on_image, heatmap)
return no_trans_heatmap, heatmap_on_image
示例2: blend_at_intersection
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def blend_at_intersection(self, images):
pre_blend_images = []
for idx, image in enumerate(images):
x_start, y_start = self.get_start_indices(idx)
image_data = np.asarray(image)
overlap_data = self.overlap_map[y_start:y_start + self.image_size, x_start:x_start + self.image_size]
alpha_image = image_data.copy()
alpha_image[:, :, 3] = image_data[:, :, 3] / overlap_data
pre_blend_image = np.zeros(self.dest_image_size + (4,), dtype=np.uint8)
pre_blend_image[y_start:y_start + self.image_size, x_start:x_start + self.image_size] = alpha_image
pre_blend_images.append(Image.fromarray(pre_blend_image, 'RGBA'))
dest_image = pre_blend_images[0]
for blend_image in pre_blend_images[1:]:
dest_image = Image.alpha_composite(dest_image, blend_image)
return dest_image
示例3: show_detections
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def show_detections(detections):
'Show image with drawn detections'
for image, detections in detections.items():
im = Image.open(image).convert('RGBA')
overlay = Image.new('RGBA', im.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(overlay)
detections.sort(key=lambda d: d['score'])
for detection in detections:
box = detection['bbox']
alpha = int(detection['score'] * 255)
draw.rectangle(box, outline=(255, 255, 255, alpha))
draw.text((box[0] + 2, box[1]), '[{}]'.format(detection['class']),
fill=(255, 255, 255, alpha))
draw.text((box[0] + 2, box[1] + 10), '{:.2}'.format(detection['score']),
fill=(255, 255, 255, alpha))
im = Image.alpha_composite(im, overlay)
im.show()
示例4: create_match_image
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def create_match_image(match):
table_border = 10
table_image = await draw_match_table(match)
image = Image.new('RGBA', (table_image.size[0] + (table_border * 2), table_image.size[1] + table_border + 64))
draw = ImageDraw.Draw(image)
draw.rectangle([0, 0, image.size[0], image.size[1]], fill=discord_color2)
draw.rectangle([0, 64, image.size[0], image.size[1]], fill=discord_color1)
image.paste(table_image, (table_border, 64))
title = TextCell(f"{'Radiant' if match['radiant_win'] else 'Dire'} Victory", font_size=48, color=("green" if match['radiant_win'] else "red"))
title.render(draw, image, 64, 0, image.size[0] - 64, 64)
team_icon = Image.open(radiant_icon if match['radiant_win'] else dire_icon).resize((64, 64))
temp_image = Image.new("RGBA", image.size)
temp_image.paste(team_icon, (0, 0))
image = Image.alpha_composite(image, temp_image)
fp = BytesIO()
image.save(fp, format="PNG")
fp.seek(0)
return fp
示例5: heatmap
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def heatmap(self, width, height, points, base_path=None, base_img=None):
"""
:param points: sequence of tuples of (x, y), eg [(9, 20), (7, 3), (19, 12)]
:return: If base_path of base_img provided, a heat map from the given points
is overlayed on the image. Otherwise, the heat map alone is returned
with a transparent background.
"""
heatmap = self.grey_heatmapper.heatmap(width, height, points)
heatmap = self._colourised(heatmap)
heatmap = _img_to_opacity(heatmap, self.opacity)
if base_path:
background = Image.open(base_path)
return Image.alpha_composite(background.convert('RGBA'), heatmap)
elif base_img is not None:
return Image.alpha_composite(base_img.convert('RGBA'), heatmap)
else:
return heatmap
示例6: remove_transparency
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def remove_transparency(img: ImageType, bg_color=DEFAULT_BG_COLOR) -> ImageType:
"""Remove alpha transparency from PNG images
Expects a PIL.Image object and returns an object of the same type with the
changes applied.
Special thanks to Yuji Tomita and Takahashi Shuuji
(https://stackoverflow.com/a/33507138)
"""
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
orig_image = img.convert('RGBA')
background = Image.new('RGBA', orig_image.size, bg_color)
img = Image.alpha_composite(background, orig_image)
return img.convert("RGB")
else:
return img
示例7: save_text_and_map_on_whitebg
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def save_text_and_map_on_whitebg(self, map):
# if no map or nothing changed
if map is None or (map == self.previous_map_no_text and
self.previous_display_text == self.display_text):
return
self.map_no_text = map
self.previous_map_no_text = self.map_no_text
self.previous_display_text = self.display_text
self.map_no_text.save(self.mapPath + '/' + self.roombaName +
'map_notext.png', "PNG")
if( self.enableMapWithText ):
final = Image.new('RGBA', self.base.size, (255,255,255,255)) # white
# paste onto a white background, so it's easy to see
final = Image.alpha_composite(final, map)
final = final.rotate(self.angle, expand=True) #(NW 12/4/2018 fixed bug causing distorted maps when rotation is not 0 - moved rotate to here)
# draw text
self.draw_text(final, self.display_text, self.fnt)
final.save(self.mapPath + '/'+self.roombaName + '_map.png', "PNG")
# try to avoid other programs reading file while writing it,
# rename should be atomic.
os.rename(self.mapPath + '/' + self.roombaName + '_map.png',
self.mapPath + '/' + self.roombaName + 'map.png')
示例8: save_text_and_map_on_whitebg
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def save_text_and_map_on_whitebg(self, map):
# if no map or nothing changed
if map is None or (map == self.previous_map_no_text and
self.previous_display_text == self.display_text):
return
self.map_no_text = map
self.previous_map_no_text = self.map_no_text
self.previous_display_text = self.display_text
self.map_no_text.save(self.mapPath + '/' + self.roombaName +
'map_notext.png', "PNG")
final = Image.new('RGBA', self.base.size, (255,255,255,255)) # white
# paste onto a white background, so it's easy to see
final = Image.alpha_composite(final, map)
# draw text
self.draw_text(final, self.display_text, self.fnt)
final.save(self.mapPath + '/'+self.roombaName + '_map.png', "PNG")
# try to avoid other programs reading file while writing it,
# rename should be atomic.
os.rename(self.mapPath + '/' + self.roombaName + '_map.png',
self.mapPath + '/' + self.roombaName + 'map.png')
示例9: calc_match_rate
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def calc_match_rate(self):
if self.match_rate > 0:
return self.match_rate
self.match_rate = 0
self.img = Image.new('RGBA', self.size)
draw = ImageDraw.Draw(self.img)
draw.polygon([(0, 0), (0, 255), (255, 255), (255, 0)], fill=(255, 255, 255, 255))
for triangle in self.triangles:
self.img = Image.alpha_composite(self.img, triangle.img_t or triangle.draw_it(self.size))
# 與下方代碼功能相同,此版本便於理解但效率低
# pixels = [self.img.getpixel((x, y)) for x in range(0, self.size[0], 2) for y in range(0, self.size[1], 2)]
# for i in range(0, min(len(pixels), len(self.target_pixels))):
# delta_red = pixels[i][0] - self.target_pixels[i][0]
# delta_green = pixels[i][1] - self.target_pixels[i][1]
# delta_blue = pixels[i][2] - self.target_pixels[i][2]
# self.match_rate += delta_red * delta_red + \
# delta_green * delta_green + \
# delta_blue * delta_blue
arrs = [np.array(x) for x in list(self.img.split())] # 分解為RGBA四通道
for i in range(3): # 對RGB通道三個矩陣分別與目標圖片相應通道作差取平方加和評估相似度
self.match_rate += np.sum(np.square(arrs[i] - self.target_pixels[i]))[0]
示例10: overlay_text
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def overlay_text(img, position, text, font, align_right=False, rectangle=False):
draw = ImageDraw.Draw(img)
w, h = font.getsize(text)
if align_right:
x, y = position
x -= w
position = (x, y)
if rectangle:
x += 1
y += 1
position = (x, y)
border = 1
rect = (x - border, y, x + w, y + h + border)
rect_img = Image.new('RGBA', (WIDTH, HEIGHT), color=(0, 0, 0, 0))
rect_draw = ImageDraw.Draw(rect_img)
rect_draw.rectangle(rect, (255, 255, 255))
rect_draw.text(position, text, font=font, fill=(0, 0, 0, 0))
img = Image.alpha_composite(img, rect_img)
else:
draw.text(position, text, font=font, fill=(255, 255, 255))
return img
示例11: stitch_map
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def stitch_map(tiles, width, height, bbox, dpi):
"""
Merge tiles together into one image.
Args:
tiles (list of dict of file): tiles for each layer
width (float): page width in mm
height (height): page height in mm
dpi (dpi): resolution in dots per inch
Returns:
PIL.Image: merged map.
"""
size = (int(width * dpi_to_dpmm(dpi)), int(height * dpi_to_dpmm(dpi)))
background = Image.new('RGBA', size, (255, 255, 255))
for layer in tiles:
layer_img = Image.new("RGBA", size)
for (x, y), tile_path in layer.items():
tile = Image.open(tile_path)
layer_img.paste(tile, ((x - bbox.min.x) * TILE_SIZE, (y - bbox.min.y) * TILE_SIZE))
background = Image.alpha_composite(background, layer_img)
add_scales_bar(background, bbox)
return background.convert("RGB")
示例12: apply_colormap_on_image
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def apply_colormap_on_image(org_im, activation, colormap_name):
"""
Apply heatmap on image
Args:
org_img (PIL img): Original image
activation_map (numpy arr): Activation map (grayscale) 0-255
colormap_name (str): Name of the colormap
"""
# Get colormap
color_map = mpl_color_map.get_cmap(colormap_name)
no_trans_heatmap = color_map(activation)
# Change alpha channel in colormap to make sure original image is displayed
heatmap = copy.copy(no_trans_heatmap)
heatmap[:, :, 3] = 0.4
heatmap = Image.fromarray((heatmap*255).astype(np.uint8))
no_trans_heatmap = Image.fromarray((no_trans_heatmap*255).astype(np.uint8))
# Apply heatmap on iamge
heatmap_on_image = Image.new("RGBA", org_im.size)
heatmap_on_image = Image.alpha_composite(heatmap_on_image, org_im.convert('RGBA'))
heatmap_on_image = Image.alpha_composite(heatmap_on_image, heatmap)
return no_trans_heatmap, heatmap_on_image
示例13: visualize_predictions
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def visualize_predictions(img, labels, font, unicode_map, fontsize=50):
"""
This function takes in a filename of an image, and the labels in the string format given in a submission csv, and returns an image with the characters and predictions annotated.
Copied from:
https://www.kaggle.com/anokas/kuzushiji-visualisation
"""
# Convert annotation string to array
labels_split = labels.split(' ')
if len(labels_split) < 3:
return img
labels = np.array(labels_split).reshape(-1, 3)
# Read image
imsource = Image.fromarray(img).convert('RGBA')
bbox_canvas = Image.new('RGBA', imsource.size)
char_canvas = Image.new('RGBA', imsource.size)
bbox_draw = ImageDraw.Draw(bbox_canvas) # Separate canvases for boxes and chars so a box doesn't cut off a character
char_draw = ImageDraw.Draw(char_canvas)
for codepoint, x, y in labels:
x, y = int(x), int(y)
char = unicode_map[codepoint] # Convert codepoint to actual unicode character
# Draw bounding box around character, and unicode character next to it
bbox_draw.rectangle((x-10, y-10, x+10, y+10), fill=(255, 0, 0, 255))
char_draw.text((x+25, y-fontsize*(3/4)), char, fill=(255, 0, 0, 255), font=font)
imsource = Image.alpha_composite(Image.alpha_composite(imsource, bbox_canvas), char_canvas)
imsource = imsource.convert("RGB") # Remove alpha for saving in jpg format.
return np.asarray(imsource)
示例14: save_prediction_image
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def save_prediction_image(_, panoptic_pred, img_info, out_dir, colors, num_stuff):
msk, cat, obj, iscrowd = panoptic_pred
img = Image.open(img_info["abs_path"])
# Prepare folders and paths
folder, img_name = path.split(img_info["rel_path"])
img_name, _ = path.splitext(img_name)
out_dir = path.join(out_dir, folder)
ensure_dir(out_dir)
out_path = path.join(out_dir, img_name + ".jpg")
# Render semantic
sem = cat[msk].numpy()
crowd = iscrowd[msk].numpy()
sem[crowd == 1] = 255
sem_img = Image.fromarray(colors[sem])
sem_img = sem_img.resize(img_info["original_size"][::-1])
# Render contours
is_background = (sem < num_stuff) | (sem == 255)
msk = msk.numpy()
msk[is_background] = 0
contours = find_boundaries(msk, mode="outer", background=0).astype(np.uint8) * 255
contours = dilation(contours)
contours = np.expand_dims(contours, -1).repeat(4, -1)
contours_img = Image.fromarray(contours, mode="RGBA")
contours_img = contours_img.resize(img_info["original_size"][::-1])
# Compose final image and save
out = Image.blend(img, sem_img, 0.5).convert(mode="RGBA")
out = Image.alpha_composite(out, contours_img)
out.convert(mode="RGB").save(out_path)
示例15: create_sample
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import alpha_composite [as 別名]
def create_sample(self):
found_bboxes = []
images = []
dominating_colour = None
for _ in range(self.numbers_per_image):
house_number_crop, bbox, median_colour = self.get_number_crop()
if len(images) > 0:
while True:
if is_close(median_colour, dominating_colour):
break
house_number_crop, bbox, median_colour = self.get_number_crop()
else:
dominating_colour = median_colour
house_number_crop, bbox = self.adjust_house_number_crop(house_number_crop, bbox)
paste_bbox = self.find_paste_location(bbox, found_bboxes)
found_bboxes.append(paste_bbox)
images.append(house_number_crop)
base_image = self.create_base_image(tuple(dominating_colour))
for image, bbox in zip(images, found_bboxes):
base_image_data = np.asarray(base_image, dtype=np.uint8).copy()
image_array = np.asarray(image, dtype=np.uint8)
image_holder = np.zeros_like(base_image_data, dtype=np.uint8)
image_holder[bbox.top:bbox.top + bbox.height, bbox.left:bbox.left + bbox.width, :] = image_array[:]
image = Image.fromarray(image_holder, mode='RGBA')
base_image_data[bbox.top:bbox.top + bbox.height, bbox.left:bbox.left + bbox.width, 3] = 255 - image_array[..., 3]
base_image = Image.fromarray(base_image_data, mode='RGBA')
base_image = Image.alpha_composite(base_image, image)
return base_image, found_bboxes