Miscs changes (Interro22)
This commit is contained in:
+44
-41
@@ -301,57 +301,61 @@ def color(score):
|
||||
green = 150 * t
|
||||
return mcolors.to_hex((red/255, green/255, 0))
|
||||
|
||||
def render_score_text(label, score, error, width_px, fontsize=18,
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
def render_score_text(label, score, error, width_px, fontsize=30,
|
||||
bg_color=(255, 255, 255, 255),
|
||||
with_error=True, id=None):
|
||||
# 2. Build highlight-text String & Properties
|
||||
# Wrap colored parts in < >
|
||||
score_str = f"{label} Note : <{score}>"
|
||||
hl_props = [{"color": color(score), "fontweight": "bold"}]
|
||||
|
||||
# 1. Build text segments: (text, color, is_bold)
|
||||
parts = []
|
||||
default_color = (0, 0, 0, 255)
|
||||
|
||||
prefix = f"{id} " if id else ""
|
||||
prefix += f"{label} Note : "
|
||||
parts.append((prefix, default_color, False))
|
||||
|
||||
parts.append((str(score), color(score), True))
|
||||
|
||||
if error and error != "null" and with_error:
|
||||
score_str += f" <{error}>"
|
||||
hl_props.append({"color": "orange", "fontweight": "bold"})
|
||||
fontsize=18
|
||||
parts.append((" ", default_color, False))
|
||||
parts.append((str(error), "orange", True))
|
||||
|
||||
if id:
|
||||
score_str = f"{id} " + score_str
|
||||
# 2. Setup Image
|
||||
height_px = 80 # roughly matches fig_height=0.8 at 100 dpi
|
||||
img = Image.new("RGBA", (int(width_px), height_px), bg_color)
|
||||
draw = ImageDraw.Draw(img)
|
||||
|
||||
# 3. Wrap Text
|
||||
dpi = 100
|
||||
fig_width = width_px / dpi
|
||||
chars_per_line = int(fig_width * 10)
|
||||
# 3. Load Fonts
|
||||
try:
|
||||
font_regular = ImageFont.truetype("DejaVuSans.ttf", fontsize)
|
||||
font_bold = ImageFont.truetype("DejaVuSans-Bold.ttf", fontsize)
|
||||
except IOError:
|
||||
# Fallback for systems without specific TTFs readily available
|
||||
print("here")
|
||||
try:
|
||||
font_regular = ImageFont.load_default(size=fontsize) # Pillow >= 10.1.0
|
||||
except TypeError:
|
||||
print("there")
|
||||
font_regular = ImageFont.load_default()
|
||||
font_bold = font_regular
|
||||
|
||||
# fig_height = 0.4 + 0.2
|
||||
fig_height = 0.8
|
||||
# 4. Draw segments horizontally
|
||||
x, y = int(width_px * 0.125), int(height_px * 0.2)
|
||||
|
||||
fig, ax = plt.subplots(figsize=(fig_width, fig_height), dpi=dpi)
|
||||
ax.axis('off')
|
||||
for text, text_color, is_bold in parts:
|
||||
f = font_bold if is_bold else font_regular
|
||||
draw.text((x, y), text, fill=text_color, font=f)
|
||||
|
||||
# Replaces plt.text
|
||||
ax_text(0.02, 0.98, score_str,
|
||||
fontsize=fontsize,
|
||||
verticalalignment='top',
|
||||
horizontalalignment='left',
|
||||
highlight_textprops=hl_props,
|
||||
ax=ax)
|
||||
# Advance X position by the width of the drawn text
|
||||
bbox = draw.textbbox((x, y), text, font=f)
|
||||
x = bbox[2]
|
||||
|
||||
buf = io.BytesIO()
|
||||
# Issues with tight bbox_inches.
|
||||
# plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0.05, transparent=True)
|
||||
plt.savefig(buf, format='png', pad_inches=0.05, transparent=True)
|
||||
plt.close(fig)
|
||||
buf.seek(0)
|
||||
|
||||
img = Image.open(buf).convert("RGBA")
|
||||
|
||||
# Apply background
|
||||
final_img = Image.new("RGBA", img.size, bg_color)
|
||||
final_img.alpha_composite(img)
|
||||
|
||||
return final_img
|
||||
return img
|
||||
|
||||
def compose_label_image(base_img, label, result, hmin,
|
||||
render_fn=render_latex_text,
|
||||
render_fn=render_real_latex_text,
|
||||
draw_callback=None,
|
||||
with_error=True,
|
||||
with_empty=False,
|
||||
@@ -391,8 +395,7 @@ def compose_label_image(base_img, label, result, hmin,
|
||||
width = base_img.width // 2
|
||||
else:
|
||||
width = base_img.width // 2 - 150
|
||||
img_score = render_score_text(label, score, error, width,
|
||||
fontsize=18, with_error=with_error,
|
||||
img_score = render_score_text(label, score, error, width, with_error=with_error,
|
||||
id=with_id)
|
||||
header_elements.append({"type": "score", "img": img_score, "data": result})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user