Skip to content

Instantly share code, notes, and snippets.

@fachinformatiker
Created May 12, 2024 20:43
Show Gist options
  • Select an option

  • Save fachinformatiker/acf22b34596aeab587e06d461e33d7e0 to your computer and use it in GitHub Desktop.

Select an option

Save fachinformatiker/acf22b34596aeab587e06d461e33d7e0 to your computer and use it in GitHub Desktop.
generate xHain Status Image for OpenEPaperLink Display
from PIL import Image, ImageDraw, ImageFont
import urllib.request, json, os, sys
width = 296
height = 128
color = "#ffffff"
logo = "https://raw.githubusercontent.com/xHain-hackspace/xHainCI/master/logo/xHain_logo-rev3.png"
tmp_file = "tmp.png"
fnt1 = ImageFont.truetype("Arial.ttf", 40)
fnt2 = ImageFont.truetype("Arial.ttf", 18)
with urllib.request.urlopen("https://x-hain.de/spaceapi/14/status") as url:
data = json.load(url)
is_open = data['state']['open']
message = data['state']['message']
if is_open == True:
is_open = "Open"
elif is_open == False:
is_open = "Closed"
else:
is_open = "Error!"
if message == "closed":
message = ":("
else:
message = message
urllib.request.urlretrieve(logo, tmp_file)
img1 = Image.new('RGB', (width, height), color)
img2 = Image.open(tmp_file)
resized_img2 = img2.resize((height, height), Image.Resampling.LANCZOS)
os.remove(tmp_file)
img1.paste(resized_img2, (0,0), mask = resized_img2)
I1 = ImageDraw.Draw(img1)
I1.text(((width + 125)/2, 40), is_open, fill=(0, 0, 0), font=fnt1, anchor="mm")
I1.text(((width + 125)/2, 80), message, fill=(0, 0, 0), font=fnt2, anchor="mm")
img1.save('xhain.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment