Proof of Concept: Displaying Multi-Line Text in Indigo

Posted on
Sun Nov 22, 2015 1:31 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Proof of Concept: Displaying Multi-Line Text in Indigo

Wouldn't work for weather alerts, but for weather states such as 'Partly Cloudy' or 'Chance of snow' which won't fit under a 96x96 icon on my control panels, I've made 3 variables for each daily/hourly device I'm displaying. I use a python loop from 1-10 and 1-24 to create the variables to save time.

Variable WeatherDay1a and WeatherDay1b hold half of the multiline state ('Chance' and 'of snow') and WeatherDay1c holds the single line state ('Overcast').

Whenever you refresh data, an embedded python script runs, looping 1-10 and 1-24 again, then saying If(Day1=Overcast), set a&b to "" and set c to Overcast. If(Day1="Chance of rain"), set a to 'Chance', b to 'of rain' and c to "". Etc etc etc.

I have a catch all at the top that sets a,b,c to "" first so if the weather is a new state that I've not added in the script yet, it displays blank.

On control pages, 'c' is displayed vertically-central where I want it, and 'a' and 'b' are lined up above each other, on top of 'c', so you either see one or the other.

Peter

Sent from my iPhone using Tapatalk

Posted on
Tue Mar 29, 2016 9:21 pm
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Proof of Concept: Displaying Multi-Line Text in Indigo

Here's yet another simple example of multi-line text that uses Matplotlib and Textwrap. This might be functionality that Karl could build into IndigoPlotD.... :D

Code: Select all
#! /usr/bin/env python2.6
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import textwrap

output_file_location = "/Users/USERNAME/Desktop/figname.png"
text_to_plot = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,'
text_to_plot = textwrap.fill(text_to_plot, 80)

plt.figure(figsize=(1,.05))
plt.text(-1, 1, text_to_plot, fontname='Lato', fontsize=9, color='white')

plt.savefig(output_file_location,
            dpi=120,
            facecolor='black',
            edgecolor='black',
            orientation=None,
            papertype=None,
            format=None,
            transparent=True,
            bbox_inches='tight',
            pad_inches=0.1,
            frameon=None,
            bbox_extra_artists=None)

And here's the output:
figname.png
figname.png (110.33 KiB) Viewed 2718 times

Replace the text_to_plot variable with a pointer to an Indigo device state or variable (and the output path and filename). I think the output doesn't look half bad.
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jan 17, 2023 6:56 am
DaveL17 offline
User avatar
Posts: 6744
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Proof of Concept: Displaying Multi-Line Text in Indigo

Here's a quick and dirty update of the PIL script that doesn't require Gnuplot and runs on Python 3. There's lots of room for improvements. Note the `imagePath` variable which is environment-specific. This should run out of the gate with Indigo 2022.

Code: Select all
"""
Could set the size of the background image based on the length of the string. This would
be in lieu of using the standard image backgrounds.
"""

from PIL import Image, ImageDraw, ImageFont
import textwrap

textToWrite = """
Now, children, come on over here. I'm going to tell you a bedtime story. Are you sitting
comfortably? Then I'll begin. Once upon a time, there lived a magnificent race of animals
that dominated the world through age after age. They ran, they swam, and they fought and
they flew, until suddenly, quite recently, they disappeared. Nature just gave up and
started again. We weren't even apes then. We were just these smart little rodents hiding
in the rocks. And when we go, nature will start over. With the bees, probably. Nature
knows when to give up, David.
"""

# Set PIL parameters.
imagePath = '/Users/Dave/Temp/'
font = ImageFont.truetype("Arial.ttf",12)
boxColor = ("#000000") # Note that this can also be an RGB tuple (123,123,123) or string "black"
textColor = ("#FFFFFF")
textWidth = 85
writeMargin = 30
writeSpacing = 15
writeSpot = 15

# Take the string and prettify it.
textToWrite = textToWrite.replace('\u000A', ' ') #replaces [line feed] with [one space]
textToWrite = textToWrite.replace('   ', ' ') #replaces [three adjacent spaces] with [one space]
textToWrite = textToWrite.replace('  ', ' ') #replaces [two adjacent spaces] with [one space]

# Determine the "shape" of the output text.
lines = textwrap.wrap(textToWrite, textWidth)
linesNum = len(lines)

# Determine the shape of the output box. We need a box which is at least one line high,
# but otherwise sized to fit the text in question.

# Box width.
if int(textWidth) > len(textToWrite):
    boxWidth = int(len(textToWrite) * 8)
else:
    boxWidth = int(textWidth * 6.4)

if boxWidth <= 250:
    boxWidth = 250

# Box height.
boxHeight = (linesNum * 25)

if boxHeight <= 40:
    boxHeight = 45

# Draw the box.
image = Image.new('RGB', (boxWidth, boxHeight), (boxColor))
draw = ImageDraw.Draw(image)

# Write the text
for line in lines:
    draw.text((writeMargin, writeSpot), line, font=font, fill=textColor)
    writeSpot += writeSpacing

# Save the file.
image.save(imagePath + "alert.png")


Edit 1: Clarifies that the script doesn't require Gnuplot but *does* run on Python 3.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Who is online

Users browsing this forum: No registered users and 1 guest

cron