Skip to content

Commit

Permalink
Merge pull request #154 from DrBlokmeister/enhance-logging-and-imagegen
Browse files Browse the repository at this point in the history
Enhance logging and imagegen
  • Loading branch information
jonasniesner authored Jun 30, 2024
2 parents acfc6b4 + daa18e3 commit ab8cb99
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
19 changes: 13 additions & 6 deletions custom_components/open_epaper_link/imagegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def customimage(entity_id, service, hass):
img = Image.new('RGBA', (canvas_width, canvas_height), color=background)
pos_y = 0
for element in payload:
_LOGGER.info("type: " + element["type"])
_LOGGER.debug("type: " + element["type"])
if not should_show_element(element):
continue
#line
Expand Down Expand Up @@ -170,13 +170,15 @@ def customimage(entity_id, service, hass):
anchor = element.get('anchor', "lt")
align = element.get('align', "left")
spacing = element.get('spacing', 5)
stroke_width = element.get('stroke_width', 0)
stroke_fill = element.get('stroke_fill', 'white')
if "max_width" in element:
text = get_wrapped_text(str(element['value']), font, line_length=element['max_width'])
anchor = None
else:
text = str(element['value'])
d.text((element['x'], akt_pos_y), text, fill=getIndexColor(color), font=font, anchor=anchor, align=align, spacing=spacing)
textbbox = d.textbbox((element['x'], akt_pos_y), text, font=font, anchor=anchor, align=align, spacing=spacing)
d.text((element['x'], akt_pos_y), text, fill=getIndexColor(color), font=font, anchor=anchor, align=align, spacing=spacing, stroke_width=stroke_width, stroke_fill=stroke_fill)
textbbox = d.textbbox((element['x'], akt_pos_y), text, font=font, anchor=anchor, align=align, spacing=spacing, stroke_width=stroke_width)
pos_y = textbbox[3]
if element["type"] == "multiline":
d = ImageDraw.Draw(img)
Expand All @@ -187,12 +189,14 @@ def customimage(entity_id, service, hass):
font = ImageFont.truetype(font_file, size)
color = element.get('color', "black")
anchor = element.get('anchor', "lm")
stroke_width = element.get('stroke_width', 0)
stroke_fill = element.get('stroke_fill', 'white')
_LOGGER.debug("Got Multiline string: %s with delimiter: %s" % (element['value'],element["delimiter"]))
lst = element['value'].replace("\n","").split(element["delimiter"])
pos = element.get('start_y', pos_y + element.get('y_padding', 10))
for elem in lst:
_LOGGER.debug("String: %s" % (elem))
d.text((element['x'], pos ), str(elem), fill=getIndexColor(color), font=font, anchor=anchor)
d.text((element['x'], pos ), str(elem), fill=getIndexColor(color), font=font, anchor=anchor, stroke_width=stroke_width, stroke_fill=stroke_fill)
pos = pos + element['offset_y']
pos_y = pos
#icon
Expand All @@ -202,7 +206,7 @@ def customimage(entity_id, service, hass):
# ttf from https://github.com/Templarian/MaterialDesign-Webfont/blob/master/fonts/materialdesignicons-webfont.ttf
font_file = os.path.join(os.path.dirname(__file__), 'materialdesignicons-webfont.ttf')
meta_file = os.path.join(os.path.dirname(__file__), "materialdesignicons-webfont_meta.json")
f = open(meta_file)
f = open(meta_file)
data = json.load(f)
chr_hex = ""
value = element['value']
Expand All @@ -219,9 +223,11 @@ def customimage(entity_id, service, hass):
break
if chr_hex == "":
raise HomeAssistantError("Non valid icon used: "+ value)
stroke_width = element.get('stroke_width', 0)
stroke_fill = element.get('stroke_fill', 'white')
font = ImageFont.truetype(font_file, element['size'])
anchor = element['anchor'] if 'anchor' in element else "la"
d.text((element['x'], element['y']), chr(int(chr_hex, 16)), fill=getIndexColor(element['color']), font=font, anchor=anchor)
d.text((element['x'], element['y']), chr(int(chr_hex, 16)), fill=getIndexColor(element['color']), font=font, anchor=anchor, stroke_width=stroke_width, stroke_fill=stroke_fill)
#dlimg
if element["type"] == "dlimg":
url = element['url']
Expand Down Expand Up @@ -491,6 +497,7 @@ def customimage(entity_id, service, hass):
rgb_image.save(buf, format='JPEG', quality="maximum")
byte_im = buf.getvalue()
return byte_im

# adds an image to the queue
def queueimg(url, content):
queue.append([url,content])
Expand Down
6 changes: 5 additions & 1 deletion docs/drawcustom/supported_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Draws text.
- **anchor** (optional) Position from the text, which shall be used as anchor. default: `lt` (left_top). Other options include `mm` (middle_middle). See https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html
- **max_width** (optional) creates line breaks in the provided text, if text is longer than `max_width` defines
- **spacing** (optional) if line breaks created in the provided text using `max_width`, set spacing between single lines. Default: 5
- **stroke_width** (optional) adds stroke around text with color `stroke_fill`. Default: 0
- **stroke_fill** (optional) the color of the added stroke. Default: white
- **align** (optional) left, center, right default: left (this sets the alignment of any **new lines**)
- **visible** (optional) show element, default: True

Expand Down Expand Up @@ -114,8 +116,10 @@ This payload takes a string and a delimiter, and will break the string on every
- **size** (optional) size of text, default: 20
- **font** (optional) name of ttf font file (see [text](#text) above for details). default: `ppb.ttf`
- **color** (optional) font color of text. default: black
- **align** (optional) left, center, right default: left (if text contains `\n` this sets the alignment of the lines)
- **spacing** (optional) if multiline text, spacing between single lines
- **stroke_width** (optional) adds stroke around text with color `stroke_fill`. Default: 0
- **stroke_fill** (optional) the color of the added stroke. Default: white
- **align** (optional) left, center, right default: left (if text contains `\n` this sets the alignment of the lines)
- **visible** (optional) show element, default: True

### line
Expand Down

0 comments on commit ab8cb99

Please sign in to comment.