Skip to content

Commit a1f67c9

Browse files
committed
examples/weather-and-light.py: fix bug with vertical label spacing.
1 parent f1508a4 commit a1f67c9

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

examples/weather-and-light.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,6 @@ def draw_background(progress, period, day):
162162
return composite
163163

164164

165-
def text_width(font, text):
166-
x1, y1, x2, y2 = font.getbbox(text)
167-
return x2 - x1
168-
169-
170165
def text_size(font, text):
171166
x1, y1, x2, y2 = font.getbbox(text)
172167
return x2 - x1, y2 - y1
@@ -384,7 +379,8 @@ def describe_light(light):
384379

385380
temp_string = f"{corr_temperature:.0f}°C"
386381
img = overlay_text(img, (68, 18), temp_string, font_lg, align_right=True)
387-
spacing = text_width(font_lg, temp_string) + 1
382+
_, text_height = text_size(font_lg, temp_string)
383+
spacing = text_height + 1
388384
if min_temp is not None and max_temp is not None:
389385
range_string = f"{min_temp:.0f}-{max_temp:.0f}"
390386
else:
@@ -398,7 +394,8 @@ def describe_light(light):
398394
corr_humidity = correct_humidity(humidity, temperature, corr_temperature)
399395
humidity_string = f"{corr_humidity:.0f}%"
400396
img = overlay_text(img, (68, 48), humidity_string, font_lg, align_right=True)
401-
spacing = text_width(font_lg, humidity_string) + 1
397+
_, text_height = text_size(font_lg, humidity_string)
398+
spacing = text_height + 1
402399
humidity_desc = describe_humidity(corr_humidity).upper()
403400
img = overlay_text(img, (68, 48 + spacing), humidity_desc, font_sm, align_right=True, rectangle=True)
404401
humidity_icon = Image.open(f"{path}/icons/humidity-{humidity_desc.lower()}.png")
@@ -408,7 +405,8 @@ def describe_light(light):
408405
light = ltr559.get_lux()
409406
light_string = f"{int(light):,}"
410407
img = overlay_text(img, (WIDTH - margin, 18), light_string, font_lg, align_right=True)
411-
spacing = text_width(font_lg, light_string.replace(",", "")) + 1
408+
_, text_height = text_size(font_lg, light_string.replace(",", ""))
409+
spacing = text_height + 1
412410
light_desc = describe_light(light).upper()
413411
img = overlay_text(img, (WIDTH - margin - 1, 18 + spacing), light_desc, font_sm, align_right=True, rectangle=True)
414412
light_icon = Image.open(f"{path}/icons/bulb-{light_desc.lower()}.png")
@@ -421,7 +419,8 @@ def describe_light(light):
421419
pressure_string = f"{int(mean_pressure):,} {trend}"
422420
img = overlay_text(img, (WIDTH - margin, 48), pressure_string, font_lg, align_right=True)
423421
pressure_desc = describe_pressure(mean_pressure).upper()
424-
spacing = text_width(font_lg, pressure_string.replace(",", "")) + 1
422+
_, text_height = text_size(font_lg, pressure_string.replace(",", ""))
423+
spacing = text_height + 1
425424
img = overlay_text(img, (WIDTH - margin - 1, 48 + spacing), pressure_desc, font_sm, align_right=True, rectangle=True)
426425
pressure_icon = Image.open(f"{path}/icons/weather-{pressure_desc.lower()}.png")
427426
img.paste(pressure_icon, (80, 48), mask=pressure_icon)

0 commit comments

Comments
 (0)