Spaces:
Running
Running
Update utils/polygon_utils.py
Browse files- utils/polygon_utils.py +13 -4
utils/polygon_utils.py
CHANGED
|
@@ -7,11 +7,20 @@ import numpy as np
|
|
| 7 |
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
import textwrap
|
| 9 |
|
| 10 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
cx = sum(x for x, _ in polygon) / len(polygon)
|
| 12 |
cy = sum(y for _, y in polygon) / len(polygon)
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
import numpy as np
|
| 17 |
import cv2
|
|
@@ -61,7 +70,7 @@ def render_translated_chunk(img: Image.Image, translations, font_path="NotoSansS
|
|
| 61 |
def draw_translated_text_convex(img, polygon_coords, text, font_path="NotoSansSC-Regular.ttf", font_scale=1.0):
|
| 62 |
from PIL import ImageDraw
|
| 63 |
font_polygon = polygon_coords
|
| 64 |
-
render_polygon =
|
| 65 |
img = inpaint_polygon(img, render_polygon, mode="auto", fallback_color=(255, 255, 255))
|
| 66 |
debug_color = (180, 255, 180)
|
| 67 |
draw = ImageDraw.Draw(img)
|
|
|
|
| 7 |
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
import textwrap
|
| 9 |
|
| 10 |
+
def shrink_or_expand_polygon(polygon, shrink_ratio=0.9):
|
| 11 |
+
"""
|
| 12 |
+
Resize a polygon around its centroid.
|
| 13 |
+
ratio < 1 → shrink
|
| 14 |
+
ratio > 1 → expand
|
| 15 |
+
"""
|
| 16 |
cx = sum(x for x, _ in polygon) / len(polygon)
|
| 17 |
cy = sum(y for _, y in polygon) / len(polygon)
|
| 18 |
+
|
| 19 |
+
new_poly = [
|
| 20 |
+
((x - cx) * ratio + cx, (y - cy) * ratio + cy)
|
| 21 |
+
for x, y in polygon
|
| 22 |
+
]
|
| 23 |
+
return [(int(x), int(y)) for x, y in new_poly]
|
| 24 |
|
| 25 |
import numpy as np
|
| 26 |
import cv2
|
|
|
|
| 70 |
def draw_translated_text_convex(img, polygon_coords, text, font_path="NotoSansSC-Regular.ttf", font_scale=1.0):
|
| 71 |
from PIL import ImageDraw
|
| 72 |
font_polygon = polygon_coords
|
| 73 |
+
render_polygon = shrink_or_expand_polygon(polygon_coords, shrink_ratio=0.9)
|
| 74 |
img = inpaint_polygon(img, render_polygon, mode="auto", fallback_color=(255, 255, 255))
|
| 75 |
debug_color = (180, 255, 180)
|
| 76 |
draw = ImageDraw.Draw(img)
|