qqwjq1981 commited on
Commit
78699c5
·
verified ·
1 Parent(s): 8c6ca99

Update utils/polygon_utils.py

Browse files
Files changed (1) hide show
  1. utils/polygon_utils.py +34 -60
utils/polygon_utils.py CHANGED
@@ -40,80 +40,54 @@ def inpaint_polygon(img: Image.Image, polygon, mode="auto", fallback_color=(255,
40
  draw.polygon(polygon, fill=fallback_color)
41
  return img_copy
42
 
43
- def draw_wrapped_text(img, polygon, text, font_path, polygon_for_size=None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  from PIL import ImageDraw, ImageFont
45
  import textwrap
46
-
47
  polygon_for_size = polygon_for_size or polygon
48
  draw = ImageDraw.Draw(img)
49
-
50
- # Bounding box from polygon for size estimation
51
  xs, ys = zip(*polygon_for_size)
52
  x_min, x_max = min(xs), max(xs)
53
  y_min, y_max = min(ys), max(ys)
54
  box_width = x_max - x_min
55
  box_height = y_max - y_min
56
-
57
- # Estimate font size from area and text length
58
- avg_char_width = 0.4 # Assumed ratio
59
  estimated_size = int(min(box_height / 1.2, box_width / (len(text) * avg_char_width)))
60
- font_size = max(10, estimated_size)
61
-
62
  font = ImageFont.truetype(font_path, font_size)
63
-
64
- # Wrap text
65
  max_chars = max(1, int(box_width / (font.getbbox("A")[2] + 1)))
66
  wrapped = textwrap.fill(text, width=max_chars)
67
-
68
- # Compute actual text size for centering
69
  bbox = draw.textbbox((0, 0), wrapped, font=font)
70
  text_w, text_h = bbox[2] - bbox[0], bbox[3] - bbox[1]
71
-
72
  x = x_min + (box_width - text_w) / 2
73
  y = y_min + (box_height - text_h) / 2
74
-
75
- draw.text((x, y), wrapped, font=font, fill="black", align="center")
76
-
77
- def draw_translated_text_convex(img, polygon_coords, text, font_path="NotoSansSC-Regular.ttf"):
78
- """
79
- Inpaints original text area, draws a light polygon boundary for debug,
80
- and renders wrapped translated text using a font size fit to the original polygon.
81
- """
82
- # Use original polygon to compute font size
83
- font_polygon = polygon_coords
84
-
85
- # Shrink polygon for rendering to avoid text overflow
86
- render_polygon = shrink_polygon(polygon_coords, shrink_ratio=0.9)
87
-
88
- # Inpaint using shrunk region
89
- img = inpaint_polygon(img, render_polygon, mode="auto", fallback_color=(255, 255, 255))
90
-
91
- # Optional: Light debug polygon (pastel green or light gray)
92
- debug_color = (180, 255, 180) # light green
93
- draw = ImageDraw.Draw(img)
94
- draw.line(render_polygon + [render_polygon[0]], fill=debug_color, width=1)
95
-
96
- # Render text inside the shrunk polygon but compute font size from original size
97
- draw_wrapped_text(img, render_polygon, text, font_path, polygon_for_size=font_polygon)
98
-
99
- return img
100
-
101
- def merge_polygons_to_convex_hull(polygons):
102
- points = [pt for poly in polygons for pt in poly]
103
- hull = MultiPoint(points).convex_hull
104
- return list(hull.exterior.coords)
105
-
106
- def render_translated_chunk(img: Image.Image, translations, font_path="NotoSansSC-Regular.ttf"):
107
- """
108
- Draws all translated text on a copy of the image using inpainting and polygon wrapping.
109
- """
110
- img_copy = img.copy()
111
-
112
- for entry in translations:
113
- polygon = entry.get("polygon") or entry.get("polygons") # accept either key
114
- text = entry.get("translated", "")
115
-
116
- if polygon and text:
117
- img_copy = draw_translated_text_convex(img_copy, polygon, text, font_path)
118
-
119
- return img_copy
 
40
  draw.polygon(polygon, fill=fallback_color)
41
  return img_copy
42
 
43
+ def merge_polygons_to_convex_hull(polygons):
44
+ points = [pt for poly in polygons for pt in poly]
45
+ hull = MultiPoint(points).convex_hull
46
+ return list(hull.exterior.coords)
47
+
48
+ def render_translated_chunk(img: Image.Image, translations, font_path="NotoSansSC-Regular.ttf", font_scale=1.0):
49
+ from PIL import ImageDraw
50
+ img_copy = img.copy()
51
+
52
+ for entry in translations:
53
+ polygon = entry.get("polygon") or entry.get("polygons")
54
+ text = entry.get("translated", "")
55
+
56
+ if polygon and text:
57
+ img_copy = draw_translated_text_convex(img_copy, polygon, text, font_path, font_scale)
58
+
59
+ return img_copy
60
+
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 = shrink_polygon(polygon_coords, shrink_ratio=0.9)
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)
68
+ draw.line(render_polygon + [render_polygon[0]], fill=debug_color, width=1)
69
+ draw_wrapped_text(img, render_polygon, text, font_path, polygon_for_size=font_polygon, font_scale=font_scale)
70
+ return img
71
+
72
+ def draw_wrapped_text(img, polygon, text, font_path, polygon_for_size=None, font_scale=1.0):
73
  from PIL import ImageDraw, ImageFont
74
  import textwrap
 
75
  polygon_for_size = polygon_for_size or polygon
76
  draw = ImageDraw.Draw(img)
 
 
77
  xs, ys = zip(*polygon_for_size)
78
  x_min, x_max = min(xs), max(xs)
79
  y_min, y_max = min(ys), max(ys)
80
  box_width = x_max - x_min
81
  box_height = y_max - y_min
82
+ avg_char_width = 0.4
 
 
83
  estimated_size = int(min(box_height / 1.2, box_width / (len(text) * avg_char_width)))
84
+ estimated_size = max(6, estimated_size)
85
+ font_size = int(estimated_size * font_scale)
86
  font = ImageFont.truetype(font_path, font_size)
 
 
87
  max_chars = max(1, int(box_width / (font.getbbox("A")[2] + 1)))
88
  wrapped = textwrap.fill(text, width=max_chars)
 
 
89
  bbox = draw.textbbox((0, 0), wrapped, font=font)
90
  text_w, text_h = bbox[2] - bbox[0], bbox[3] - bbox[1]
 
91
  x = x_min + (box_width - text_w) / 2
92
  y = y_min + (box_height - text_h) / 2
93
+ draw.text((x, y), wrapped, font=font, fill="black", align="center")