Python PIL.Image.alpha_composite() Examples
The following are 30
code examples of PIL.Image.alpha_composite().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
PIL.Image
, or try the search function
.

Example #1
Source File: viz_functional.py From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #2
Source File: create_svhn_dataset_4_images.py From stn-ocr with GNU General Public License v3.0 | 6 votes |
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
Example #3
Source File: utils.py From retinanet-examples with BSD 3-Clause "New" or "Revised" License | 6 votes |
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()
Example #4
Source File: drawdota.py From MangoByte with MIT License | 6 votes |
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
Example #5
Source File: heatmap.py From heatmappy with MIT License | 6 votes |
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
Example #6
Source File: create_svhn_dataset_4_images.py From see with GNU General Public License v3.0 | 6 votes |
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
Example #7
Source File: img_aux_processing.py From optimize-images with MIT License | 6 votes |
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
Example #8
Source File: roomba.py From Roomba980-Python with MIT License | 6 votes |
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')
Example #9
Source File: roomba.py From Roomba980-Python with MIT License | 6 votes |
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')
Example #10
Source File: gaimage.py From bdfz.courses with GNU General Public License v3.0 | 6 votes |
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]
Example #11
Source File: weather-and-light.py From enviroplus-python with MIT License | 6 votes |
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
Example #12
Source File: print.py From geos with BSD 3-Clause "New" or "Revised" License | 6 votes |
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")
Example #13
Source File: misc_functions.py From pytorch-cnn-visualizations with MIT License | 6 votes |
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
Example #14
Source File: vis_submission.py From kuzushiji-recognition with MIT License | 5 votes |
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)
Example #15
Source File: test_panoptic.py From seamseg with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #16
Source File: create_svhn_dataset.py From stn-ocr with GNU General Public License v3.0 | 5 votes |
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
Example #17
Source File: drawdota.py From MangoByte with MIT License | 5 votes |
def get_neutral_image(item): background = Image.new("RGBA", (64, 64)) size = background.size circle_diameter = 48 circle_thickness = 4 img_scale = circle_diameter / 64 inner_radius = circle_diameter / 2 inner_circle = ((size[0] / 2) - inner_radius, (size[1] / 2) - inner_radius, (size[0] / 2) + inner_radius, (size[1] / 2) + inner_radius) outer_radius = inner_radius + circle_thickness outer_circle = ((size[0] / 2) - outer_radius, (size[1] / 2) - outer_radius, (size[0] / 2) + outer_radius, (size[1] / 2) + outer_radius) if item: draw = ImageDraw.Draw(background) draw.ellipse(outer_circle, fill="#393939") item_img = await get_item_image(item) item_img = item_img.resize((int(item_img.size[0] * img_scale), int(item_img.size[1] * img_scale))) item_img = item_img.crop(( math.floor((item_img.size[0] - background.size[0]) / 2), math.floor((item_img.size[1] - background.size[1]) / 2), item_img.size[0] - math.ceil((item_img.size[0] - background.size[0]) / 2), item_img.size[1] - math.ceil((item_img.size[1] - background.size[1]) / 2)) ) mask_circle = Image.new("RGBA", background.size) mask_draw = ImageDraw.Draw(mask_circle) mask_draw.ellipse(inner_circle, fill="#ffffff") temp_image = Image.new("RGBA", (64, 64)) temp_image.paste(item_img, (0, 0), mask=mask_circle) return Image.alpha_composite(background, temp_image) else: return background
Example #18
Source File: imagetools.py From MangoByte with MIT License | 5 votes |
def paste_image(image1, image2, x, y): temp_image = Image.new("RGBA", image1.size) temp_image.paste(image2, (x, y)) return Image.alpha_composite(image1, temp_image) # colors an image with one single color for all the pixes that are currently not transparent
Example #19
Source File: viz.py From kaggle-kuzushiji-2019 with MIT License | 5 votes |
def visualize_training_data( image_path: Path, labels: List[Tuple[str, int, int, int, int]], fontsize: int = 50, with_labels: bool = True): """ This function takes in a filename of an image, and the labels in the string format given in train.csv, and returns an image containing the bounding boxes and characters annotated. """ # Read image img = Image.open(image_path).convert('RGBA') if len(labels) == 0: return img bbox_canvas = Image.new('RGBA', img.size) char_canvas = Image.new('RGBA', img.size) # Separate canvases for boxes and chars so a box doesn't cut off a character bbox_draw = ImageDraw.Draw(bbox_canvas) char_draw = ImageDraw.Draw(char_canvas) font = load_font(fontsize) for codepoint, x, y, w, h in labels: x, y, w, h = int(x), int(y), int(w), int(h) char = UNICODE_MAP.get(codepoint, codepoint) # Draw bounding box around character, and unicode character next to it bbox_draw.rectangle((x, y, x+w, y+h), fill=(255, 255, 255, 0), outline=(255, 0, 0, 255), width=4) if with_labels: char_draw.text((x + w + fontsize/4, y + h/2 - fontsize), char, fill=(0, 0, 255, 255), font=font) img = Image.alpha_composite(Image.alpha_composite(img, bbox_canvas), char_canvas) img = img.convert('RGB') # Remove alpha for saving in jpg format. return img
Example #20
Source File: functions.py From ttkwidgets with GNU General Public License v3.0 | 5 votes |
def overlay(image, color): """ Overlay a rectangle of color (RGBA) on the image and return the result. """ width, height = image.size im = Image.new("RGBA", (width, height), color) preview = Image.alpha_composite(image, im) return preview
Example #21
Source File: alphabar.py From ttkwidgets with GNU General Public License v3.0 | 5 votes |
def _draw_gradient(self, alpha, color): """Draw the gradient and put the cursor on alpha.""" self.delete("gradient") self.delete("cursor") del self.gradient width = self.winfo_width() height = self.winfo_height() bg = create_checkered_image(width, height) r, g, b = color w = width - 1. gradient = Image.new("RGBA", (width, height)) for i in range(width): for j in range(height): gradient.putpixel((i, j), (r, g, b, round2(i / w * 255))) self.gradient = ImageTk.PhotoImage(Image.alpha_composite(bg, gradient), master=self) self.create_image(0, 0, anchor="nw", tags="gardient", image=self.gradient) self.lower("gradient") x = alpha / 255. * width h, s, v = rgb_to_hsv(r, g, b) if v < 50: fill = "gray80" else: fill = 'black' self.create_line(x, 0, x, height, width=2, tags='cursor', fill=fill)
Example #22
Source File: test_image.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_alpha_composite(self): # https://stackoverflow.com/questions/3374878 # Arrange from PIL import ImageDraw expected_colors = sorted([ (1122, (128, 127, 0, 255)), (1089, (0, 255, 0, 255)), (3300, (255, 0, 0, 255)), (1156, (170, 85, 0, 192)), (1122, (0, 255, 0, 128)), (1122, (255, 0, 0, 128)), (1089, (0, 255, 0, 0))]) dst = Image.new('RGBA', size=(100, 100), color=(0, 255, 0, 255)) draw = ImageDraw.Draw(dst) draw.rectangle((0, 33, 100, 66), fill=(0, 255, 0, 128)) draw.rectangle((0, 67, 100, 100), fill=(0, 255, 0, 0)) src = Image.new('RGBA', size=(100, 100), color=(255, 0, 0, 255)) draw = ImageDraw.Draw(src) draw.rectangle((33, 0, 66, 100), fill=(255, 0, 0, 128)) draw.rectangle((67, 0, 100, 100), fill=(255, 0, 0, 0)) # Act img = Image.alpha_composite(dst, src) # Assert img_colors = sorted(img.getcolors()) self.assertEqual(img_colors, expected_colors)
Example #23
Source File: submission.py From generative-art with MIT License | 5 votes |
def overlay(filename, overlay_filename, save_filename): bg = Image.open(filename).convert("RGBA") overlay = Image.open(overlay_filename).convert("RGBA") #bg.paste(overlay, (0, 0), overlay) #bg.show() Image.alpha_composite(bg, overlay).save(save_filename)
Example #24
Source File: create_svhn_dataset.py From see with GNU General Public License v3.0 | 5 votes |
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
Example #25
Source File: bbox_plotter.py From see with GNU General Public License v3.0 | 5 votes |
def render_text(self, dest_image, text): label_image = Image.new(dest_image.mode, dest_image.size) # only keep ascii characters # labels = ''.join(filter(lambda x: len(x) == len(x.encode()), labels)) draw = ImageDraw.Draw(label_image) text_width, text_height = draw.textsize(text, font=self.font) draw.rectangle([dest_image.width - text_width - 1, 0, dest_image.width, text_height], fill=(255, 255, 255, 160)) draw.text((dest_image.width - text_width - 1, 0), text, fill='green', font=self.font) dest_image = Image.alpha_composite(dest_image, label_image) return dest_image
Example #26
Source File: test.py From seg-mentor with MIT License | 5 votes |
def segment_movie(fcnfunc, checkpoint, video_file_in, pixels=None): from PIL import Image from moviepy.editor import VideoFileClip image_ph = tf.placeholder(tf.int32) # , shape = smth w. pixels (doesn't work..:) image_t3d, predictions = prepare_graph(fcnfunc, image_ph, pixels) with tf.Session() as sess: sess.run(tf.local_variables_initializer()) tf.train.Saver().restore(sess, checkpoint) #ext = '.mp4' ext = video_file_in[video_file_in.find('.') :] video_file_out = video_file_in.replace(ext, '_segmented'+ext) video_file_out = video_file_out.replace('.avi', '.mp4') #print video_file_out input_clip = VideoFileClip(video_file_in) mask_alpha = round(0.3*255) colors = np.random.random([21, 3]) colors -= np.min(colors, axis=1)[:, np.newaxis] colors /= np.max(colors, axis=1)[:, np.newaxis] colors *= 255 colors = np.concatenate(( np.round(colors), mask_alpha*np.ones((21,1))), axis=1) background_class = 0 # TODO what about other cases? colors[background_class][3] = 0 def process_frame(image_in): scaled_image, inferred_pixel_labels = sess.run([image_t3d, predictions], {image_ph: image_in}) seg_mask = np.take(colors, inferred_pixel_labels, axis=0) # print seg_mask.shape, type(seg_mask) image_in_walpha = np.concatenate((scaled_image, (255-mask_alpha)*np.ones(scaled_image.shape[:2]+(1,))), axis=2) # print inferred_pixel_labels.shape, seg_mask.shape, image_in_walpha.shape # print np.min(rescaled_image), np.max(rescaled_image) composite = Image.alpha_composite(Image.fromarray(np.uint8(image_in_walpha)), Image.fromarray(np.uint8(seg_mask))) composite_backscaled = composite.resize(image_in.shape[1::-1], Image.LANCZOS) return np.array(composite_backscaled)[:,:,:3] annotated_clip = input_clip.fl_image(process_frame) annotated_clip.write_videofile(video_file_out, audio=False)
Example #27
Source File: roomba.py From Roomba980-Python with MIT License | 5 votes |
def transparent_paste(self, base_image, icon, position): ''' needed because PIL pasting of transparent imges gives weird results ''' image = Image.new('RGBA', self.base.size, self.transparent) image.paste(icon,position) base_image = Image.alpha_composite(base_image, image) return base_image
Example #28
Source File: roomba.py From Roomba980-Python with MIT License | 5 votes |
def transparent_paste(self, base_image, icon, position): ''' needed because PIL pasting of transparent imges gives weird results ''' image = Image.new('RGBA', self.base.size, self.transparent) image.paste(icon,position) base_image = Image.alpha_composite(base_image, image) return base_image
Example #29
Source File: bbox_plotter.py From kiss with GNU General Public License v3.0 | 5 votes |
def render_text(self, dest_image, source_image, text, i, bottom=False): label_image = Image.new(dest_image.mode, dest_image.size) draw = ImageDraw.Draw(label_image) paste_width = (i + 1) * source_image.width text_width, text_height = draw.textsize(text, self.font) insert_height = source_image.height - text_height - 1 if bottom else 0 draw.rectangle([paste_width - text_width - 1, insert_height, paste_width, insert_height + text_height], fill=(255, 255, 255, 160)) draw.text((paste_width - text_width - 1, insert_height), text, fill='green', font=self.font) dest_image = Image.alpha_composite(dest_image, label_image) return dest_image
Example #30
Source File: bbox_plotter.py From kiss with GNU General Public License v3.0 | 5 votes |
def render_objectness_scores(self, objectness_scores, dest_image, image, bottom=False): if len(objectness_scores) == 0: return dest_image if isinstance(objectness_scores, tuple) and self.plot_objectness_classification_result: objectness_scores = objectness_scores[1] objectness_scores = F.softmax(objectness_scores) objectness_classification = map(lambda x: "{:.2f}".format(float(x)), objectness_scores[:, 1].data) elif isinstance(objectness_scores, tuple) and not self.plot_objectness_classification_result: objectness_scores = objectness_scores[0] objectness_classification = map(lambda x: "{:.2f}".format(float(F.mean(x).data)), objectness_scores[:, 0].data) elif objectness_scores.shape[-1] == 2: objectness_scores = F.softmax(objectness_scores) objectness_classification = map(lambda x: "{:.2f}".format(float(x)), objectness_scores[:, 1].data) else: objectness_classification = map(lambda x: "{:.2f}".format(float(F.mean(x).data)), objectness_scores[:, 0].data) for i, char in enumerate(objectness_classification, start=1): label_image = Image.new(dest_image.mode, dest_image.size) draw = ImageDraw.Draw(label_image) paste_width = (i + 1) * image.width text_width, text_height = draw.textsize(char, self.font) insert_height = image.height - text_height - 1 if bottom else 0 draw.rectangle([paste_width - text_width - 1, insert_height, paste_width, insert_height + text_height], fill=(255, 255, 255, 160)) draw.text((paste_width - text_width - 1, insert_height), char, fill='green', font=self.font) dest_image = Image.alpha_composite(dest_image, label_image) return dest_image