N / ignored_preview.py
q6's picture
Show character-shaped text mask overlay in ignored preview
aa4871b
Raw History Blame Contribute Delete
856 Bytes
from PIL import Image, ImageDraw
TEXT_OVERLAY_COLOR = "#6d9"
TEXT_OVERLAY_OPACITY = 0.55
BOX_WIDTH = 2
BOX_COLORS = {
"eyes": "#6ad",
"hands": "#a9e",
"feet": "#fa6",
}
def save_ignored_preview(input_path, output_path, regions, text_mask=None):
with Image.open(input_path) as source:
preview = source.convert("RGB")
if text_mask is not None:
overlay = Image.new("RGB", preview.size, TEXT_OVERLAY_COLOR)
tinted = Image.blend(preview, overlay, TEXT_OVERLAY_OPACITY)
preview = Image.composite(tinted, preview, text_mask)
draw = ImageDraw.Draw(preview)
for part, boxes in regions.items():
for x1, y1, x2, y2 in boxes:
draw.rectangle(
(x1, y1, x2 - 1, y2 - 1), outline=BOX_COLORS[part], width=BOX_WIDTH,
)
preview.save(output_path, format="PNG")