Closed mschmermer closed 3 weeks ago
i found a better solution.
add a white border around the text by move the text with white color around the default text by:
def draw_text(image: np.ndarray, blk_list: List[TextBlock], font_pth: str, init_font_size, colour: str = "#000"):
image = cv2_to_pil(image)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(font_pth, size=init_font_size)
for blk in blk_list:
x1, y1, width, height = blk.xywh
tbbox_top_left = (x1, y1)
translation = blk.translation
if not translation or len(translation) == 1:
continue
translation, font_size = pil_word_wrap(image, tbbox_top_left, font_pth, init_font_size, translation, width, height, align=blk.alignment, spacing=blk.line_spacing)
font = font.font_variant(size=font_size)
offsets = [(dx, dy) for dx in (-2, -1, 0, 1, 2) for dy in (-2, -1, 0, 1, 2) if dx != 0 or dy != 0]
for dx, dy in offsets:
draw.multiline_text((tbbox_top_left[0] + dx, tbbox_top_left[1] + dy), translation, font=font, fill="#FFF", align=blk.alignment, spacing=1)
draw.multiline_text(tbbox_top_left, translation, colour, font, align=blk.alignment, spacing=1)
image = pil_to_cv2(image)
return image
You should create a pull request
I will do that if i have time. Feature is ready and tested so far ...
@mschmermer Thanks, I've implemented your fix. I've tested it and i agree it works better than trying to detect the font color so i'll close this.
Description: Translated text sometimes appears in black on dark backgrounds, making it hard to read.
Problem: When the background is dark, the translated text is placed in black, causing readability issues.
Solution:
Analyze the background color of the text area. Adjust the text color for sufficient contrast. Use light text on dark backgrounds and dark text on light backgrounds.