| | from PIL import Image, ImageDraw, ImageFont |
| | import random |
| |
|
| | def visualize_masks(image, masks, mask_prompts, output_path, font_size=35, use_random_colors=False): |
| | |
| | overlay = Image.new('RGBA', image.size, (0, 0, 0, 0)) |
| | |
| | colors = [ |
| | (165, 238, 173, 80), |
| | (76, 102, 221, 80), |
| | (221, 160, 77, 80), |
| | (204, 93, 71, 80), |
| | (145, 187, 149, 80), |
| | (134, 141, 172, 80), |
| | (157, 137, 109, 80), |
| | (153, 104, 95, 80), |
| | (165, 238, 173, 80), |
| | (76, 102, 221, 80), |
| | (221, 160, 77, 80), |
| | (204, 93, 71, 80), |
| | (145, 187, 149, 80), |
| | (134, 141, 172, 80), |
| | (157, 137, 109, 80), |
| | (153, 104, 95, 80), |
| | ] |
| | |
| | if use_random_colors: |
| | colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 80) for _ in range(len(masks))] |
| | |
| | |
| | try: |
| | font = ImageFont.truetype("arial", font_size) |
| | except IOError: |
| | font = ImageFont.load_default(font_size) |
| |
|
| | |
| | for mask, mask_prompt, color in zip(masks, mask_prompts, colors): |
| | |
| | mask_rgba = mask.convert('RGBA') |
| | mask_data = mask_rgba.getdata() |
| | new_data = [(color if item[:3] == (255, 255, 255) else (0, 0, 0, 0)) for item in mask_data] |
| | mask_rgba.putdata(new_data) |
| |
|
| | |
| | draw = ImageDraw.Draw(mask_rgba) |
| | mask_bbox = mask.getbbox() |
| | text_position = (mask_bbox[0] + 10, mask_bbox[1] + 10) |
| | draw.text(text_position, mask_prompt, fill=(255, 255, 255, 255), font=font) |
| |
|
| | |
| | overlay = Image.alpha_composite(overlay, mask_rgba) |
| | |
| | |
| | result = Image.alpha_composite(image.convert('RGBA'), overlay) |
| | |
| | |
| | result.save(output_path) |
| |
|
| | return result |
| |
|