fintic-tracker/stockTicker.py
2021-10-05 20:29:05 +01:00

2243 lines
87 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Copyright (C) 2020 Fintic, finticofficial@gmail.com
#
# This file is part of Fintic project, developed by Neythen Treloar and Justin Dunn
#
# stockTicker can not be copied and/or distributed without the express
# permission of Fintic
import sys, select
import os
import threading
from PIL import Image, ImageDraw, ImageFont, ImageSequence
Image.init()
import time
import csv
import requests
import pexpect
from rgbmatrix import RGBMatrix, RGBMatrixOptions
from rgbmatrix.graphics import *
from multiprocessing import Process
import traceback
import json
from datetime import datetime
import matplotlib.colors as mcolors
def getInput(Block=False):
if Block or select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
msg = sys.stdin.read(1)
#sys.stdin.flush()
else:
msg = ''
return msg
class StockTicker():
def __init__(self):
#Define global resources
self.symbols = []
self.greenORred = (255, 255, 255)
#self.blank = Image.open('logos/blank.png')
self.blank = Image.new('RGB', (10, 32))
self.running = True
self.brightness = 1.0
self.delay = 0.02
# Configuration for the matrix
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
options.chain_length = 2
options.parallel = 1
options.hardware_mapping = 'adafruit-hat' # If you have an Adafruit HAT: 'adafruit-hat'
options.gpio_slowdown = 4
self.matrix = RGBMatrix(options = options)
self.points = True # display crypto change in points or percent
self.functions = {'Stocks': self.getStockImage, 'Crypto': self.getCryptoImage, 'Forex': self.getForexImage,
'Daily Forecast':self.getDailyWeatherImage, 'Current Weather': self.getTodayWeatherImage,
'Sports (Team Stats)':lambda : self.getLeagueTableImage('premier_league'), 'Sports (Past Games)': lambda:self.getLeagueImage('NBA', 'past'),
'Sports (Upcoming Games)': lambda : self.getLeagueImage('NHL', 'future'), 'Sports (Live Games)': lambda: self.getLeagueImage('NBA', 'live'),
'News':self.getNewsImage, 'Custom Messages': self.getUserText, 'Custom Images': self.getUserImage, 'Custom GIFs':self.getUserGIF,
'Stocks Prof': self.getStockProfessional, 'Crypto Prof': self.getCryptoProfessional, 'Forex Prof': self.getForexProfessional,
'Current Weather Prof': self.getTodayWeatherProfessional, 'News Prof':self.getNewsProfessional}
def openImage(self, image_file):
image = Image.open(image_file)
# Make image fit our screen.
#image.thumbnail((self.matrix.width, self.matrix.height), Image.ANTIALIAS)
#image = image.convert('RGB')
return image
def degreesToCompass(self, deg):
dirs = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']
return dirs[int((deg+22.5)//45)%8]
def setImage(self, image, offset_x = 0, offset_y = 0, unsafe=True, min_x = 0, max_x = 128, min_y = 0, max_y = 32):
if (image.mode != "RGB"):
image = image.convert('RGB')
if unsafe:
#In unsafe mode we directly acceshow to send commands to running python processs the underlying PIL image array
#in cython, which is considered unsafe pointer accecss,
#however it's super fast and seems to work fine
#https://groups.google.com/forum/#!topic/cython-users/Dc1ft5W6KM4
img_width, img_height = image.size
self.matrix.SetPixelsPillow(offset_x, offset_y, img_width, img_height, image)
else:
# First implementation of a SetImage(). OPTIMIZE_ME: A more native
# implementation that directly reads the buffer and calls the underlying
# C functions can certainly be faster.
img_width, img_height = image.size
pixels = image.load()
for y in range(max(0, -offset_y), min(img_height, self.matrix.height - offset_y)):
for x in range(max(0, -offset_x), min(img_width, self.matrix.width - offset_x)):
if min_x <= x + offset_x <= max_x and min_y <= y + offset_y <= max_y:
(r, g, b) = pixels[x, y]
self.matrix.SetPixel(x + offset_x, y + offset_y, r*self.brightness, g*self.brightness, b*self.brightness)
def scrollImage(self, image, offset_x = 0, offset_y = 0, frame_skip = 10, gif = False, pause_frames = 0):
img_width, img_height = image.size
kill = False
while offset_x > -(img_width+1):
if offset_x == 0:
while pause_frames > 0:
if pause_frames%frame_skip == 0:
self.incrementGIF(image)
pause_frames -=1
if gif:
self.double_buffer.SetImage(image.convert('RGB'), offset_x, offset_y)
else:
self.double_buffer.SetImage(image, offset_x, offset_y)
for y in range(self.matrix.height):
self.matrix.SetPixel(offset_x + img_width +1 , y , 0,0,0)
self.matrix.SetPixel(offset_x + img_width , y , 0,0,0)
self.double_buffer = self.matrix.SwapOnVSync(self.double_buffer)
time.sleep(self.delay)
kill = self.checkKilled()
if kill: break
if kill: break
# for animation in gifs
if offset_x%frame_skip == 0:
self.incrementGIF(image)
#image = image.convert('RGB')
offset_x -= 1
#self.setImage(image.convert('RGB'), offset_x = offset_x, offset_y = offset_y)
if gif:
self.double_buffer.SetImage(image.convert('RGB'), offset_x, offset_y)
else:
self.double_buffer.SetImage(image, offset_x, offset_y)
buff = 0
# remove the ppixels behind the image, to stop trailing
self.double_buffer = self.matrix.SwapOnVSync(self.double_buffer)
for y in range(self.matrix.height):
self.matrix.SetPixel(offset_x + img_width +1 , y , 0,0,0)
self.matrix.SetPixel(offset_x + img_width , y , 0,0,0)
time.sleep(self.delay)
kill = self.checkKilled()
if kill: break
return kill
def scrollImageY(self, image, direction = 1, offset_x = 0, offset_y = 0, frame_skip = 10, gif = False):
kill = False
while offset_y != 0:
# for animation in gifs
if offset_y%frame_skip == 0:
self.incrementGIF(image)
offset_y += direction
if gif:
self.double_buffer.SetImage(image.convert('RGB'), offset_x, offset_y)
else:
self.double_buffer.SetImage(image, offset_x, offset_y)
self.double_buffer = self.matrix.SwapOnVSync(self.double_buffer)
time.sleep(self.delay)
kill = self.checkKilled()
if kill: break
return kill
def scrollImageTransition(self, image_files, offset_x = 0, offset_y = 0, stocks = True):
# use two image files and switch between them with a seemless transition
current_img = 1
image1 = self.openImage(image_files[0]).convert('RGB')
image2 = self.openImage(image_files[1]).convert('RGB')
double_buffer = self.matrix.CreateFrameCanvas()
kill = False
while True:
if current_img == 1:
if stocks:
update_process = Process(target = self.getFullStockImage, args = (1,))
update_process.start()
image1 = self.openImage(image_files[0]).convert('RGB')
image2 = self.openImage(image_files[1]).convert('RGB')
elif current_img == 2:
if stocks:
update_process = Process(target = self.getFullStockImage, args = (2,))
update_process.start()
image1 = self.openImage(image_files[1]).convert('RGB')
image2 = self.openImage(image_files[0]).convert('RGB')
img_width, img_height = image1.size
while offset_x > -img_width:
offset_x -= 1
double_buffer.SetImage(image1, offset_x, offset_y)
if offset_x + img_width < self.matrix.width: # if the image is ending
double_buffer.SetImage(image2, offset_x, offset_y)
double_buffer = self.matrix.SwapOnVSync(double_buffer)
time.sleep(self.delay)
kill = self.checkKilled()
if kill: break
if stocks:
image1.close()
image2.close()
if kill: break
if stocks:
update_process.join()
if current_img == 1:
current_img = 2
elif current_img == 2:
current_img = 1
offset_x = 0
def scrollImageStacked(self, image, offset_x = 0, offset_y = 0):
img_width, img_height = image.size
while offset_x > -img_width - 128:
offset_x -= 1
self.setImage(image, offset_x = offset_x+128, offset_y = offset_y)
self.setImage(image, offset_x = offset_x, offset_y = offset_y+20)
kill = self.checkKilled()
time.sleep(self.delay)
return kill
def updateMultiple(self, options):
for option in options:
img = self.functions[option]()
if option not in ['Custom GIFs']: # aving the gif like this kills the animation
img.save('./display_images/'+ option+ '.ppm')
def incrementGIF(self, image):
try:
image.seek(self.frame)
except EOFError:
self.frame = 0
image.seek(self.frame)
self.frame +=1
def checkKilled(self):
kill = False
try:
msg = getInput()
if msg == 'K':
self.resetMatrix()
kill = True
self.process_msg(msg)
except KeyboardInterrupt:
sys.stdout.flush()
pass
return kill
def scrollFunctionsAnimated(self, options, animation = 'down'):
# scrolls trhough all functions with animation. Updates functions and remakes images when each function not being dispplayed
self.updateMultiple([options[0]])
kill = False
i = 0 # keep track of which image we are displaying
self.double_buffer = self.matrix.CreateFrameCanvas()
while True:
update_process = Process(target = self.updateMultiple, args = ([options[(i+1) % len(options)]],))
update_process.start()
if options[i % len(options)] != 'Custom GIFs':
image = self.openImage('./display_images/' + options[i % len(options)] +'.ppm')
image = image.convert('RGB')
else:
image = self.openImage('./display_images/Custom GIFs.gif')
img_width, img_height = image.size
offset_x = 0
if animation == 'traditional':
offset_x = 128
elif animation == 'continuous':
offset_x = 0
elif animation in ['up', 'down']:
offset_x = max(0, 128-img_width)
offset_y = 0
#first scroll image in from bottom
frame_skip = int((1/15)/self.delay) #controls how fast gifs run
self.frame = 0
pause_frames = int(0.5/self.delay)
if animation == 'up':
offset_y = 33
direction = -1
kill = self.scrollImageY(image, direction = direction, offset_x = offset_x, offset_y = offset_y, frame_skip = frame_skip, gif = options[i % len(options)] == 'Custom GIFs')
elif animation == 'down':
direction = 1
offset_y = -33
kill = self.scrollImageY(image, direction = direction, offset_x = offset_x, offset_y = offset_y, frame_skip = frame_skip, gif = options[i % len(options)] == 'Custom GIFs')
if kill: break
offset_y = 0
if animation in ['up', 'down']:
while pause_frames > 0:
if pause_frames%frame_skip == 0:
self.incrementGIF(image)
pause_frames -=1
if options[i % len(options)] != 'Custom GIFs':
self.double_buffer.SetImage(image, offset_x, offset_y)
else:
self.double_buffer.SetImage(image.convert('RGB'), offset_x, offset_y)
self.double_buffer = self.matrix.SwapOnVSync(self.double_buffer)
time.sleep(self.delay)
kill = self.checkKilled()
if kill: break
if kill: break
if kill: break
kill = self.scrollImage(image, offset_x = offset_x, offset_y = offset_y, frame_skip = frame_skip, gif = options[i % len(options)] == 'Custom GIFs')
if kill: break
update_process.join()
i+=1
def scrollMultiple(self, animation = 'down'):
# scrolls trhough all functions with animation. Updates functions and remakes images when each function not being dispplayed
# read lines from csv
images = []
delays = []
kinds = []
f = open('csv/multiple.csv', 'r')
CSV = csv.reader(f)
next(CSV)
font = ImageFont.load("./fonts/texgyre-27.pil")
for row in CSV:
kind, content, delay = row
delays.append(delay)
kinds.append(kind)
print(kind, content, delay)
if kind == 'text':
images.append(self.textImage(content, font, 255, 255, 0, True, w_buff = 50))
elif kind == 'image':
images.append(self.openImage(content).convert('RGB'))
elif kind == 'gif':
images.append(self.openImage(content))
f.close()
kill = False
i = 0 # keep track of which image we are displaying
self.double_buffer = self.matrix.CreateFrameCanvas()
while True:
image = images[i%len(images)]
delay = delays[i%len(images)]
kind = kinds[i%len(images)]
img_width, img_height = image.size
offset_x = 0
if animation == 'traditional':
offset_x = 128
elif animation == 'continuous':
offset_x = 0
elif animation in ['up', 'down']:
offset_x = max(0, 128-img_width)
offset_y = 0
#first scroll image in from bottom
frame_skip = int((1/15)/self.delay) #controls how fast gifs run
self.frame = 0
pause_frames = int(float(delay)/self.delay)
if animation == 'up':
offset_y = 33
direction = -1
kill = self.scrollImageY(image, direction = direction, offset_x = offset_x, offset_y = offset_y, frame_skip = frame_skip, gif = kind=='gif')
elif animation == 'down':
direction = 1
offset_y = -33
kill = self.scrollImageY(image, direction = direction, offset_x = offset_x, offset_y = offset_y, frame_skip = frame_skip, gif = kind=='gif')
offset_y = 0
if kill: break
kill = self.scrollImage(image, offset_x = offset_x, offset_y = offset_y, frame_skip = frame_skip, gif = kind=='gif', pause_frames = pause_frames)
if kill: break
i+=1
def textImage(self, text, font, r = 255, g = 255, b = 255, matrix_height = False, w_buff = 3, h_buff = 3):
'''
creates and returns a ppm image containing the text in the supplied font and colour
'''
width, height = self.get_text_dimensions(text, font)
if matrix_height:
height = 32
img = Image.new('RGB', (width + w_buff, height + h_buff))
d = ImageDraw.Draw(img)
d.text((0, 0), text, fill=(r, g, b), font=font)
return img
def getUserText(self):
'''
displays the text entered in the webpage by the user.
'''
f = open('csv/scroll_text.csv', 'r')
CSV = csv.reader(f)
text, r, g, b = next(CSV)
f.close()
title_img = self.openImage('feature_titles/message.png')
font = ImageFont.load("./fonts/texgyre-27.pil")
img = self.textImage(text, font, int(r), int(g), int(b), True, w_buff = 50)
return self.stitchImage([title_img, img])
def displayGIF(self, gif):
# To iterate through the entire gif
i = 0
while True:
try:
gif.seek(i)
except EOFError:
i = 0
gif.seek(i)
# do something to im
self.setImage(gif.convert('RGB'))
time.sleep(0.5)
i += 1
try:
msg = getInput()
if msg == 'K':
gif.close()
self.resetMatrix()
break
self.process_msg(msg)
except KeyboardInterrupt:
sys.stdout.flush()
pass
def scrollGIF(self, gif, offset_x = 0, offset_y = 0):
# To iterate through the entire gif
i = 0
img_width, img_height = gif.size
while offset_x > -img_width:
offset_x -= 1
try:
gif.seek(i)
except EOFError:
i = 0
gif.seek(i)
# do something to im
self.setImage(gif.convert('RGB'), offset_x = offset_x)
time.sleep(self.delay)
if offset_x % 20 == 0:
i += 1
for x in range(offset_x + img_width, 128):
for y in range(self.matrix.height):
self.matrix.SetPixel(x , y , 0,0,0)
kill = self.checkKilled()
if kill:
break
return kill
#Using change between min and day price give appropriate arrow
#and set the overall change colour
def getArrow(self, CHANGE, professional = False):
self.greenORred
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'stocks')
if(CHANGE>0):
Arrow = Image.open(os.path.join(logos_path, 'up.png'))
self.greenORred = (0, 255, 0)
else:
Arrow = Image.open(os.path.join(logos_path, 'down.png'))
self.greenORred = (255, 0, 0)
CHANGE = (CHANGE * -1)
if professional:
w, h = Arrow.size
Arrow = Arrow.resize((int(w/2), int(h/2)))
return Arrow, CHANGE
def get_text_dimensions(self, text_string, font):
canvas = Image.new('RGB', (10000,100))
draw = ImageDraw.Draw(canvas)
monospace = font
text = text_string
white = (255,255,255)
draw.text((0, 0), text, font=monospace, fill=white)
bbox = canvas.getbbox()
width = bbox[2]-bbox[0]
height = bbox[3]-bbox[1]
return width,height
#Draw Ticker, current and change onto one image
def textToImage(self, TICKER, CURRENT, CHANGE, ARROW, font = ImageFont.load("./fonts/10x20.pil")):
text_width_current, text_height = self.get_text_dimensions(CURRENT, font)
img = Image.new('RGB', (text_width_current +100 , 32))
d = ImageDraw.Draw(img)
d.text((4, 0), TICKER, fill=(255, 255, 255), font=font)
d.text((4, 16), CURRENT, fill=self.greenORred, font=font)
img.paste(ARROW, ((text_width_current + 9),18))
d.text(((text_width_current+29), 16), CHANGE, fill=self.greenORred, font=font)
text_width_change, text_height = self.get_text_dimensions(CHANGE, font)
newWidth = text_width_current + text_width_change +30
img = img.crop((0,0,newWidth,32))
return img
def textToImageProf(self, TICKER, CURRENT, CHANGE, ARROW, font):
text_width_current, text_height = self.get_text_dimensions(CURRENT, font)
img = Image.new('RGB', (text_width_current +100 , 32))
d = ImageDraw.Draw(img)
d.text((4, 0), TICKER, fill=(255, 255, 255), font=font)
d.text((4, 8), CURRENT, fill=self.greenORred, font=font)
img.paste(ARROW, ((text_width_current + 7),10))
d.text(((text_width_current+18), 8), CHANGE, fill=self.greenORred, font=font)
text_width_change, text_height = self.get_text_dimensions(CHANGE, font)
newWidth = text_width_current + text_width_change +30
img = img.crop((0,0,newWidth,32))
return img
#Stitch the logo & prices picture into one image
def stitchImage(self, image_list):
widths, heights = zip(*(i.size for i in image_list))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in image_list:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
return new_im
def resetMatrix(self):
for x in range(self.matrix.width):
for y in range(self.matrix.height):
self.matrix.SetPixel(x , y , 0,0,0)
def getCryptoImage(self):
f = open('csv/crypto_settings.json', 'r')
all_crypto_settings = json.load(f)
f.close()
if all_crypto_settings['title']:
title_img = self.openImage('feature_titles/crypto.png')
image_list = [title_img]
image_list.append(self.blank)
else:
image_list = []
coin_info = all_crypto_settings['symbols']
coin_bases = list(coin_info.keys())
for i, cb in enumerate(coin_bases):
ticker, base = cb.split(',')
current = coin_info[cb]["current"]
change = coin_info[cb]["24hr_change"]
if all_crypto_settings["point"]:
# convert percent to points
change = change/100 * current
current = '%.2f' % current
arrow, change = self.getArrow(change)
change = '%.2f' % change
midFrame = self.textToImage(ticker + '(' + base + ')', current, change, arrow) #IMAGE THE TEXT
try:
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'crypto')
logo = Image.open(os.path.join(logos_path, ticker + '.png'))
stitchedStock = self.stitchImage([logo,midFrame])
except:
stitchedStock = midFrame
image_list.append(stitchedStock)
image_list.append(self.blank)
finalDisplayImage = self.stitchImage(image_list)
return finalDisplayImage
def getCryptoProfessional(self):
self.blank = Image.new('RGB', (0, 16))
f = open('csv/crypto_settings.json', 'r')
all_crypto_settings = json.load(f)
f.close()
if all_crypto_settings['title']:
title_img = self.openImage('feature_titles/small_feature_titles/crypto.png')
image_list = [title_img]
image_list.append(self.blank)
else:
image_list = []
coin_info = all_crypto_settings['symbols']
coin_bases = list(coin_info.keys())
for i, cb in enumerate(coin_bases):
ticker, base = cb.split(',')
current = coin_info[cb]["current"]
change = coin_info[cb]["24hr_change"]
if all_crypto_settings["point"]:
# convert percent to points
change = change/100 * current
current = '%.2f' % current
arrow, change = self.getArrow(change, professional=True)
change = '%.2f' % change
midFrame = self.textToImageProf(ticker + '(' + base + ')', current, change, arrow, font=ImageFont.load("./fonts/6x10.pil")) #IMAGE THE TEXT
try:
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'crypto')
logo = Image.open(os.path.join(logos_path, ticker + '.png'))
w,h = logo.size
logo=logo.resize((int(w/2), int(h/2)))
stitchedStock = self.stitchImage([logo,midFrame])
except:
stitchedStock = midFrame
image_list.append(stitchedStock)
image_list.append(self.blank)
finalDisplayImage = self.stitchImage(image_list)
self.blank = Image.new('RGB', (10, 32))
return finalDisplayImage
def getForexImage(self):
f = open('csv/forex_settings.json', 'r')
all_forex_settings = json.load(f)
f.close()
if all_forex_settings['title']:
title_img = self.openImage('feature_titles/forex.png')
image_list = [title_img]
image_list.append(self.blank)
else:
image_list = []
forex_settings = all_forex_settings['symbols']
symbol_bases = list(forex_settings.keys())
for i, sb in enumerate(symbol_bases):
symbol, base = sb.split(',')
current = forex_settings[sb]['current']
change = forex_settings[sb]['24hr_change']
current = '%.3f' % current
arrow, change = self.getArrow(change)
change = '%.6f' % change
midFrame = self.textToImage(symbol+ '(' + base + ')', current, change, arrow) #IMAGE THE TEXT
try:
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'currencies')
logo = Image.open(os.path.join(logos_path, symbol.lower() + '.png'))
bse = Image.open(os.path.join(logos_path, base.lower() + '.png'))
new_im = Image.new('RGB', (32, 32))
new_im.paste(bse, (0,10), bse.convert('RGBA'))
new_im.paste(logo, (10,0), logo.convert('RGBA'))
stitchedStock = self.stitchImage([new_im, midFrame])
image_list.append(new_im)
except Exception as e:
print(e)
image_list.append(midFrame)
image_list.append(self.blank)
finalDisplayImage = self.stitchImage(image_list)
return finalDisplayImage
def getForexProfessional(self):
self.blank = Image.new('RGB', (0, 16))
f = open('csv/forex_settings.json', 'r')
all_forex_settings = json.load(f)
f.close()
if all_forex_settings['title']:
title_img = self.openImage('feature_titles/small_feature_titles/forex.png')
image_list = [title_img]
image_list.append(self.blank)
else:
image_list = []
forex_settings = all_forex_settings['symbols']
symbol_bases = list(forex_settings.keys())
for i, sb in enumerate(symbol_bases):
symbol, base = sb.split(',')
current = forex_settings[sb]['current']
change = forex_settings[sb]['24hr_change']
current = '%.3f' % current
arrow, change = self.getArrow(change, professional = True)
change = '%.6f' % change
midFrame = self.textToImageProf(symbol + '(' + base + ')', current, change, arrow, font = ImageFont.load("./fonts/6x10.pil")) #IMAGE THE TEXT
try:
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'currencies')
logo = Image.open(os.path.join(logos_path, symbol.lower() + '.png'))
bse = Image.open(os.path.join(logos_path, base.lower() + '.png'))
new_im = Image.new('RGB', (32, 32))
new_im.paste(bse, (0,10), bse.convert('RGBA'))
new_im.paste(logo, (10,0), logo.convert('RGBA'))
width, height = new_im.size
new_im = new_im.resize((int(width/2), int(height/2)))
stitchedStock = self.stitchImage([new_im, midFrame])
image_list.append(new_im)
except Exception as e:
print(e)
image_list.append(midFrame)
image_list.append(self.blank)
finalDisplayImage = self.stitchImage(image_list)
self.blank = Image.new('RGB', (10, 32))
return finalDisplayImage
def getStockImage(self):
f = open('csv/stocks_settings.json', 'r')
all_stocks_settings = json.load(f)
f.close()
if all_stocks_settings['title']:
title_img = self.openImage('feature_titles/stocks.png')
image_list = [title_img]
image_list.append(self.blank)
else:
image_list = []
stock_info = all_stocks_settings['symbols']
symbols = list(stock_info.keys())
for i, symbol in enumerate(symbols):
info = stock_info[symbol]
change = float(info['current'])-float(info['opening']) #TEXT
ticker = symbol #TEXT
current = '%.2f' % float(info['current']) #TEXT
arrow, change = self.getArrow(change)
change = '%.2f' % change
midFrame = self.textToImage(ticker, current, change, arrow) #IMAGE THE TEXT
try:
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'stocks')
logo = Image.open(os.path.join(logos_path, ticker + '.png'))
stitchedStock = self.stitchImage([logo,midFrame])
except:
stitchedStock = midFrame
image_list.append(stitchedStock)
image_list.append(self.blank)
finalDisplayImage = self.stitchImage(image_list)
return finalDisplayImage
def getStockProfessional(self):
self.blank = Image.new('RGB', (0, 16))
f = open('csv/stocks_settings.json', 'r')
all_stocks_settings = json.load(f)
f.close()
if all_stocks_settings['title']:
title_img = self.openImage('feature_titles/small_feature_titles/stocks.png')
image_list = [title_img, Image.new('RGB', (5, 16))]
image_list.append(self.blank)
else:
image_list = []
stock_info = all_stocks_settings['symbols']
symbols = list(stock_info.keys())
for i, symbol in enumerate(symbols):
info = stock_info[symbol]
change = float(info['current'])-float(info['opening']) #TEXT
ticker = symbol #TEXT
current = '%.2f' % float(info['current']) #TEXT
arrow, change = self.getArrow(change, professional=True)
change = '%.2f' % change
midFrame = self.textToImageProf(ticker, current, change, arrow, font=ImageFont.load("./fonts/6x10.pil")) #IMAGE THE TEXT
try:
try: #load the tiny logo
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'tiny_stocks')
logo = Image.open(os.path.join(logos_path, ticker + '.png'))
except: # load the big logo and scale it
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'stocks')
logo = Image.open(os.path.join(logos_path, ticker + '.png'))
# half the size of the logo
width, height = logo.size
logo = logo.resize((int(width/2), int(height/2)))
stitchedStock = self.stitchImage([logo,midFrame])
except Exception as e:
stitchedStock = midFrame
image_list.append(stitchedStock)
image_list.append(self.blank)
finalDisplayImage = self.stitchImage(image_list)
self.blank = Image.new('RGB', (10, 32))
return finalDisplayImage
def getNewsImage(self):
all_settings = json.load(open('csv/news_settings.json', 'r'))
if all_settings['title']:
title_img = self.openImage('feature_titles/news.png')
image_list = [title_img]
headline_font = ImageFont.load("./fonts/6x13.pil")
source_font = ImageFont.load("./fonts/6x13.pil")
headline_info = all_settings['headlines']
headlines = []
source_date_times = []
sources = []
for i, hi in enumerate(headline_info):
headline, source, date_time = hi
date, time = date_time.split('T')
time = time[:-1]
source_date_time = source + ': ' + date + ' ' + time
headline = headline.replace("^", ",")
headline = headline.replace("", "'")
headline = headline.replace("", "'")
headline = headline.replace('', '"')
headline = headline.replace('', '"')
headline = headline.replace('', '-')
headline = headline.replace('', '-')
headline = ''.join([h for h in headline if ord(h) < 256])
lst = headline.rsplit('-', 1) #remove source name at end of headline
headline = lst[0]
headline_img = self.textImage(headline, headline_font, matrix_height = True)
source_img = self.textImage(source_date_time, source_font, r=255, g=255, b=0, matrix_height = True)
try:
logo_name = sources.lower().replace(' ', '-')
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'news_logos')
logo = Image.open(os.path.join(logos_path, logo_name + '.png'))
except Exception as e:
logo_name = 'default'
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'news_logos')
logo = Image.open(os.path.join(logos_path, logo_name + '.png'))
img = Image.new('RGB', (headline_img.size[0], 32))
img.paste(headline_img, (2, 0))
img.paste(source_img, (2,16))
img= self.stitchImage([logo,img])
image_list.append(img)
image_list.append(self.blank)
news_image = self.stitchImage(image_list)
return news_image
def getNewsProfessional(self):
all_settings = json.load(open('csv/news_settings.json', 'r'))
if all_settings['title']:
title_img = self.openImage('feature_titles/small_feature_titles/news.png')
image_list = [title_img]
headline_font = ImageFont.load("./fonts/6x13.pil")
source_font = ImageFont.load("./fonts/6x13.pil")
headline_info = all_settings['headlines']
headlines = []
source_date_times = []
sources = []
blank = Image.new('RGB', (0, 16))
for i, hi in enumerate(headline_info):
headline, source, date_time = hi
date, time = date_time.split('T')
time = time[:-1]
source_date_time = source + ': ' + date + ' ' + time
headline = headline.replace("^", ",")
headline = headline.replace("", "'")
headline = headline.replace("", "'")
headline = headline.replace('', '"')
headline = headline.replace('', '"')
headline = headline.replace('', '-')
headline = headline.replace('', '-')
headline = ''.join([h for h in headline if ord(h) < 256])
lst = headline.rsplit('-', 1) #remove source name at end of headline
headline = lst[0]
headline_img = self.textImage(headline, headline_font, matrix_height = True)
source_img = self.textImage(source_date_time+ ':', source_font, r=255, g=255, b=0, matrix_height = True)
try:
logo_name = sources[i].lower().replace(' ', '-')
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'news_logos')
logo = Image.open(os.path.join(logos_path, logo_name + '.png'))
except Exception as e:
logo_name = 'default'
logos_path = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logos'), 'news_logos')
logo = Image.open(os.path.join(logos_path, logo_name + '.png'))
width, height = logo.size
logo = logo.resize((int(width/2), int(height/2)))
img = Image.new('RGB', (headline_img.size[0]+ source_img.size[0] + logo.size[0] +5, 32))
img.paste(headline_img, (source_img.size[0]+ logo.size[0] -5, 3))
img.paste(source_img, (2,3))
img= self.stitchImage([logo,img])
image_list.append(img)
image_list.append(blank)
news_image = self.stitchImage(image_list)
return news_image
def getLeagueImage(self, league=False, time = 'past'):
if time in ['past', 'live']:
filepath = 'csv/{}_games.json'.format(time)
else:
filepath = 'csv/upcoming_games.json'
all_settings = json.load(open(filepath, 'r'))
title_img = self.openImage('feature_titles/sports_'+ time + '.png')
if all_settings['title']:
title_img = self.openImage('feature_titles/sports_{}.png'.format(time))
imgs = [title_img, self.blank]
else:
imgs = []
leagues_info = all_settings['leagues']
leagues = list(leagues_info.keys())
for league in leagues:
img = Image.new('RGB', (10000, 32))
league_info = json.load(open('csv/sports/{}/{}_games.json'.format(league, time), 'r'))
small_font = ImageFont.load("./fonts/5x7.pil")
med_font = ImageFont.load("./fonts/7x14B.pil")
large_font = ImageFont.load("./fonts/9x18B.pil")
sports_info = self.readSportsCSV(league)
buff_size = 25
x_offset = 0
for match in league_info[-15:]:
home_team = match['home_team']
away_team = match['away_team']
home_score = match['home_score']
away_score = match['away_score']
date = match['date'].replace('-', '.')
#rond = match['round']
try:
home_logo = Image.open('logos/sports/{}/{}'.format(league, sports_info[home_team]['logo']))
except Exception as e:
home_logo = self.textImage(home_team.replace(' ', '\n'), small_font, r = 255, g = 255, b = 255)
try:
away_logo = Image.open('logos/sports/{}/{}'.format(league, sports_info[away_team]['logo']))
except Exception as e:
away_logo = self.textImage(away_team.replace(' ', '\n'), small_font, r = 255, g = 255, b = 255)
date_timage = self.textImage(date, small_font, r = 255, g = 255, b = 255)
img.paste(home_logo, (x_offset,0))
x_offset += home_logo.size[0] + 2
if time == 'future':
img.paste(date_timage, (x_offset+5, 0))
h_colour = mcolors.to_rgb(sports_info[home_team]['colour'].replace(' ', ''))
a_colour = mcolors.to_rgb(sports_info[away_team]['colour'].replace(' ', ''))
hc_timage = self.textImage(sports_info[home_team]['code'], med_font, r = int(h_colour[0]*255), g = int(h_colour[1]*255), b = int(h_colour[2]*255))
ac_timage = self.textImage(sports_info[away_team]['code'], med_font, r = int(a_colour[0]*255), g = int(a_colour[1]*255), b = int(a_colour[2]*255))
vs_timage = self.textImage('vs', med_font, r = 255, g = 255, b = 255, h_buff = 5)
img.paste(hc_timage, (x_offset, 9))
img.paste(vs_timage, (x_offset + hc_timage.size[0], 9))
img.paste(ac_timage, (x_offset + hc_timage.size[0] + vs_timage.size[0], 9))
x_offset += max( date_timage.size[0], hc_timage.size[0] + vs_timage.size[0] + ac_timage.size[0])
else:
score_image = self.textImage(home_score + '-' + away_score, large_font, h_buff = 5, r = 255, g = 255, b = 255)
#vs_timage = self.textImage(sports_info[home_team]['code'] + 'vs' + sports_info[away_team]['code'], small_font, r = 255, g = 255, b = 255)
h_colour = mcolors.to_rgb(sports_info[home_team]['colour'].replace(' ', ''))
a_colour = mcolors.to_rgb(sports_info[away_team]['colour'].replace(' ', ''))
hc_timage = self.textImage(sports_info[home_team]['code'], small_font, r = int(h_colour[0]*255), g = int(h_colour[1]*255), b = int(h_colour[2]*255))
ac_timage = self.textImage(sports_info[away_team]['code'], small_font, r = int(a_colour[0]*255), g = int(a_colour[1]*255), b = int(a_colour[2]*255))
vs_timage = self.textImage('vs', small_font, r = 255, g = 255, b = 255)
if date_timage.size[0] > score_image.size[0]:
img.paste(date_timage, (x_offset+2, 0))
img.paste(hc_timage, (x_offset+6, 9))
img.paste(vs_timage, (x_offset+5 + hc_timage.size[0], 9))
img.paste(ac_timage, (x_offset+6 + hc_timage.size[0] + vs_timage.size[0], 9))
img.paste(score_image, (x_offset + 2 + int((date_timage.size[0] - score_image.size[0])/2), 15))
else:
img.paste(date_timage, (x_offset+1+int((score_image.size[0] - date_timage.size[0] )/2), 0))
vs_size = hc_timage.size[0] + vs_timage.size[0] + ac_timage.size[0]
img.paste(hc_timage, (x_offset + 1 + int((score_image.size[0] - vs_size)/2), 9))
img.paste(vs_timage, (x_offset + int((score_image.size[0] - vs_size)/2) + hc_timage.size[0], 9))
img.paste(ac_timage, (x_offset+1 + int((score_image.size[0] - vs_size)/2) + hc_timage.size[0] + vs_timage.size[0], 9))
img.paste(score_image, (x_offset+1, 15))
x_offset += max( date_timage.size[0]+4, hc_timage.size[0] + vs_timage.size[0] + ac_timage.size[0]+4, 2 + int(score_image.size[0]))
#img.paste(vs_timage, (x_offset+4, 9))
#if league == 'NHL':
#
#img.paste(round_timage, (x_offset+ 7, 8))
#x_offset += max(home_timage.size[0], away_timage.size[0], date_timage.size[0], round_timage.size[0], score_image.size[0])
img.paste(away_logo, (x_offset,0))
x_offset += away_logo.size[0]
x_offset += buff_size
img = img.crop((0,0,x_offset ,32))
return self.stitchImage([title_img, img])
def getLeagueTableImage(self, league = False):
all_settings = json.load(open('csv/league_tables.json', 'r'))
leagues_info = all_settings['leagues']
leagues = list(leagues_info.keys())
if all_settings['title']:
title_img = self.openImage('feature_titles/sports_team_stats.png')
imgs = [title_img, self.blank]
else:
imgs = []
for league in leagues:
img = Image.new('RGB', (10000, 32))
team_info = leagues_info[league]
small_font = ImageFont.load("./fonts/5x7.pil")
med_font = ImageFont.load("./fonts/8x13B.pil")
large_font = ImageFont.load("./fonts/10x20.pil")
#if league =='NHL': # read the NHl info from the csv, prem will need this as well
sports_info = self.readSportsCSV(league) # gets colour and symbol info etc from csv
buff_size = 20
x_offset = 0
for team in team_info:
try:
logo = Image.open('logos/sports/{}/{}'.format(league, sports_info[team['name']]['logo']))
img.paste(logo, (x_offset, 0))
x_offset += logo.size[0] + 2
except Exception as e:
print('no logo for:', team['name'])
name_timage = self.textImage(team['name'], med_font, r = 255, g = 255, b = 0)
wins_timage = self.textImage('Wins:' + team['wins'], small_font, r = 0, g = 255, b = 0)
loss_timage = self.textImage('Losses:' + team['loss'], small_font, r = 255, g = 0, b = 0)
draw_timage = self.textImage('Draws:' + team['draw'], small_font, r = 0, g = 0, b = 255)
points_timage = self.textImage('Points:' + team['points'], small_font, r = 255, g = 255, b = 255)
standing_timage = self.textImage('Standing:' + team['standing'], small_font, r = 255, g = 255, b = 255)
img.paste(name_timage, (x_offset, -1))
img.paste(wins_timage, (x_offset, 12))
img.paste(loss_timage, (x_offset, 19))
img.paste(draw_timage, (x_offset, 26))
x_offset += max( wins_timage.size[0], loss_timage.size[0], draw_timage.size[0])
img.paste(points_timage, (x_offset, 14))
img.paste(standing_timage, (x_offset, 22))
x_offset += max(points_timage.size[0], standing_timage.size[0]) + buff_size
img = img.crop((0,0,x_offset ,32))
return self.stitchImage([title_img, img])
def getTodayWeatherImage(self):
all_settings = json.load(open('csv/current_weather.json', 'r'))
if all_settings['title']:
title_img = self.openImage('feature_titles/weather.png')
imgs = [title_img, self.blank]
else:
imgs = []
current_weathers = all_settings['locations']
locations = list(current_weathers.keys())
for i, location in enumerate(locations):
img = Image.new('RGB', (203, 32))
current_weather = current_weathers[location]
small_font = ImageFont.load("./fonts/5x7.pil")
large_font = ImageFont.load("./fonts/10x20.pil")
location_img = self.textImage(location.upper(), small_font, r = 255, g = 255, b = 0)
img.paste(location_img, (5,0))
main = current_weather['main_weather']
if main == 'Clouds':
main = current_weather['description']
weather_ids = {'Clear': '01', 'few clouds': '02', 'scattered clouds': '03', 'broken clouds':'04', 'overcast clouds':'04', 'Drizzle':'09',
'Rain':'10', 'Thunderstorm':'11', 'Snow':'13', 'Mist': '50', 'Smoke': '50', 'Haze': '50', 'Dust': '50', 'Fog': '50',
'Sand': '50', 'Ash': '50', 'Squall': '50', 'Tornado': '50'}
weather_dir = './logos/weather_icons'
weather_img = Image.open(weather_dir + '/weather_type_icons/' + weather_ids[main] + '.png')
img.paste(weather_img, (5,9))
temp_img = self.textImage(str("{0:.0f}".format(current_weather['temp'])), large_font)
img.paste(temp_img, (39,9))
deg_img = self.textImage('o', small_font)
img.paste(deg_img, (59, 8))
main = current_weather['main_weather']
main_img = self.textImage(main.upper(), small_font)
img.paste(main_img, (35, 26))
feels_img = self.textImage('Feels like:'.upper() + str("{0:.0f}".format(current_weather['feels_like'])), small_font)
img.paste(feels_img, (location_img.size[0] + 10, 0))
min_img = self.textImage( "{0:.0f}".format(current_weather['min_temp']), small_font, r=0, g=0, b=255)
img.paste(min_img, (66, 12))
max_img = self.textImage( "{0:.0f}".format(current_weather['max_temp']), small_font, r=255, g=0, b=0)
img.paste(max_img, (66, 21))
hum_img = Image.open(weather_dir + '/humidity.png')
img.paste(hum_img, ( 82, 8))
htext_img = self.textImage(str(current_weather['humidity']) + '%', small_font)
img.paste(htext_img, (95, 10))
uv_img = Image.open(weather_dir + '/uv.png')
img.paste(uv_img, ( 82, 22))
utext_img = self.textImage(str(round(current_weather['uv'], 1)) , small_font)
img.paste(utext_img, (95, 23))
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
months =['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
month = months[int(datetime.now().strftime('%m'))-1]
date = str(int(datetime.now().strftime('%d')))
weekday = weekdays[datetime.today().weekday()]
date_img = self.textImage((month + ' ' + date + ',' + weekday).upper(), small_font)
img.paste(date_img, (132, 0))
rain_img = Image.open(weather_dir + '/rain-chance.png')
img.paste(rain_img, (118,8))
rtext_img = self.textImage(str(int(current_weather['rain_chance']*100)) + '%', small_font)
img.paste(rtext_img, (131, 10))
cloud_img = Image.open(weather_dir + '/clouds.png')
img.paste(cloud_img, (118,20))
ctext_img = self.textImage(str(current_weather['clouds']) + '%', small_font)
img.paste(ctext_img, (131, 22))
wind_img = Image.open(weather_dir + '/wind.png')
img.paste(wind_img, (154,8))
wtext_img = self.textImage("{0:.0f}".format(current_weather['wind_speed']) + 'm/s'.upper(), small_font)
img.paste(wtext_img, (168, 10))
wdir_img = self.textImage(self.degreesToCompass(current_weather['wind_direction']).upper(), small_font)
img.paste(wdir_img, (191, 10))
vis_img = Image.open(weather_dir + '/visibility.png')
img.paste(vis_img, (154,20))
vtext_img = self.textImage(str(round(current_weather['visibility']/1000, 1)) + 'km'.upper(), small_font)
img.paste(vtext_img, (168, 22))
imgs.append(img)
imgs.append(self.blank)
return self.stitchImage(imgs)
def getTodayWeatherProfessional(self):
all_settings = json.load(open('csv/current_weather.json', 'r'))
if all_settings['title']:
title_img = self.openImage('feature_titles/small_feature_titles/weather.png')
image_list = [title_img, Image.new('RGB', (3, 16))]
else:
image_list = []
current_weathers = all_settings['locations']
locations = list(current_weathers.keys())
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
months =['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
month = months[int(datetime.now().strftime('%m'))-1]
date = str(int(datetime.now().strftime('%d')))
weekday = weekdays[datetime.today().weekday()]
for i, location in enumerate(locations):
img = Image.new('RGB', (1000, 32))
current_weather = current_weathers[location]
small_font = ImageFont.load("./fonts/4x6.pil")
font = ImageFont.load("./fonts/6x10.pil")
large_font = ImageFont.load("./fonts/10x20.pil")
main = current_weather['main_weather']
if main == 'Clouds':
main = current_weather['description']
weather_ids = {'Clear': '01', 'few clouds': '02', 'scattered clouds': '03', 'broken clouds':'04', 'overcast clouds':'04', 'Drizzle':'09',
'Rain':'10', 'Thunderstorm':'11', 'Snow':'13', 'Mist': '50', 'Smoke': '50', 'Haze': '50', 'Dust': '50', 'Fog': '50',
'Sand': '50', 'Ash': '50', 'Squall': '50', 'Tornado': '50'}
weather_dir = './logos/weather_icons'
location_img = self.textImage(location.upper(), font, r = 255, g = 255, b = 0)
img.paste(location_img, (5,3))
x_offset = location_img.size[0] + 8
date_img = self.textImage((month + ' ' + date + ',' + weekday).upper(), font)
img.paste(date_img, (x_offset, 3))
x_offset += date_img.size[0] + 2
weather_img = Image.open(weather_dir + '/weather_type_icons/' + weather_ids[main] + '.png')
w, h = weather_img.size
weather_img = weather_img.resize((int(w/2), int(h/2)))
main = current_weather['main_weather']
main_img = self.textImage(main.upper(), font)
img.paste(main_img, (x_offset, 3))
x_offset += main_img.size[0] + 2
img.paste(weather_img, (x_offset,3))
x_offset += weather_img.size[0] + 2
temp_img = self.textImage(str("{0:.0f}".format(current_weather['temp'])), font)
img.paste(temp_img, (x_offset,3))
x_offset += temp_img.size[0]-3
deg_img = self.textImage('o', small_font)
img.paste(deg_img, (x_offset+1, 1))
x_offset += deg_img.size[0] -2
min_img = self.textImage( "{0:.0f}".format(current_weather['min_temp']), small_font, r=0, g=0, b=255)
img.paste(min_img, (x_offset+2, 2))
max_img = self.textImage( "{0:.0f}".format(current_weather['max_temp']), small_font, r=255, g=0, b=0)
img.paste(max_img, (x_offset+2, 8))
x_offset += max_img.size[0] + 2
hum_img = Image.open(weather_dir + '/humidity.png')
htext_img = self.textImage(str(current_weather['humidity']) + '%', font)
uv_img = Image.open(weather_dir + '/uv.png')
utext_img = self.textImage(str(round(current_weather['uv'], 1)), font)
rain_img = Image.open(weather_dir + '/rain-chance.png')
rtext_img = self.textImage(str(int(current_weather['rain_chance']*100)) + '%', font)
cloud_img = Image.open(weather_dir + '/clouds.png')
ctext_img = self.textImage(str(current_weather['clouds']) + '%', font)
wind_img = Image.open(weather_dir + '/wind.png')
wtext_img = self.textImage("{0:.0f}".format(current_weather['wind_speed']) + 'm/s', font)
wdir_img = self.textImage(self.degreesToCompass(current_weather['wind_direction']).upper(), font)
vis_img = Image.open(weather_dir + '/visibility.png')
vtext_img = self.textImage(str(current_weather['visibility']/1000) + 'km', font)
for image in [hum_img, htext_img, uv_img, utext_img, rain_img, rtext_img, cloud_img, ctext_img, wind_img, wtext_img, wdir_img, vis_img, vtext_img]:
img.paste(image, (x_offset, 3))
x_offset += image.size[0] + 2
img = img.crop((0,0,x_offset +image.size[0] ,16))
image_list.append(img)
return self.stitchImage(image_list)
def getDailyWeatherImageAlt(self):
f = open( "csv/weather_location.txt", 'r' )
location = f.read()
f.close()
img = Image.new('RGB', (128, 32))
current_weather = json.load(open('csv/current_weather.json', 'r'))
small_font = ImageFont.load("./fonts/5x7.pil")
extra_small_font = ImageFont.load("./fonts/4x6.pil")
large_font = ImageFont.load("./fonts/10x20.pil")
location_img = self.textImage(location, extra_small_font, r = 255, g = 255, b = 0)
main = current_weather['main_weather']
if main == 'Clouds':
main = current_weather['description']
weather_ids = {'Clear': '01', 'few clouds': '02', 'scattered clouds': '03', 'broken clouds':'04', 'overcast clouds':'04', 'Drizzle':'09',
'Rain':'10', 'Thunderstorm':'11', 'Snow':'13', 'Mist': '50', 'Smoke': '50', 'Haze': '50', 'Dust': '50', 'Fog': '50',
'Sand': '50', 'Ash': '50', 'Squall': '50', 'Tornado': '50'}
weather_dir = './logos/weather_icons'
weather_img = Image.open(weather_dir + '/weather_type_icons/' + weather_ids[main] + '.png')
temp_img = self.textImage(str("{0:.0f}".format(current_weather['temp'])), large_font)
deg_img = self.textImage('o', small_font)
min_img = self.textImage( "{0:.0f}".format(current_weather['min_temp']), small_font, r=0, g=0, b=255)
max_img = self.textImage( "{0:.0f}".format(current_weather['max_temp']), small_font, r=255, g=0, b=0)
main = current_weather['main_weather']
main_img = self.textImage(main, small_font)
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
months =['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
month = months[int(datetime.now().strftime('%m'))]
date = str(int(datetime.now().strftime('%d')))
weekday = weekdays[datetime.today().weekday()]
date_img = self.textImage(month + ' ' + date + ',' + weekday, extra_small_font)
rain_img = Image.open(weather_dir + '/rain-chance.png')
rtext_img = self.textImage(str(int(current_weather['rain_chance']*100)) + '%', extra_small_font)
hum_img = Image.open(weather_dir + '/humidity.png')
htext_img = self.textImage(str(current_weather['humidity']) + '%', extra_small_font)
uv_img = Image.open(weather_dir + '/uv.png')
utext_img = self.textImage(str(current_weather['uv']) , extra_small_font)
wind_img = Image.open(weather_dir + '/wind.png')
wtext_img = self.textImage(str(current_weather['wind_speed']) + 'm/s', extra_small_font)
img.paste(location_img, (0,0))
img.paste(weather_img, (0,12))
img.paste(temp_img, (30,9))
img.paste(deg_img, (50, 8))
img.paste(min_img, (55, 10))
img.paste(main_img, (30, 26))
img.paste(max_img, (55, 19))
img.paste(date_img, (location_img.size[0], 1))
img.paste(rain_img, (75,0))
img.paste(rtext_img, (88, 1))
img.paste(hum_img, (102, 0))
img.paste(htext_img, (113, 1))
#img.paste(uv_img, ( 96, 0))
#img.paste(utext_img, (109, 0))
#img.paste(wind_img, (96,0))
#img.paste(wtext_img, (109,0))
img0 = img
img = Image.new('RGB', (1000, 32))
daily_weather = json.load(open('csv/daily_weather.json', 'r'))
#img.paste(date_img, (70, 0))
x_offset = 64
for i in range(0,len(daily_weather)-1):
weekday = weekdays[(datetime.today().weekday() + i)%7]
day_img = self.textImage( weekday, small_font)
weather = daily_weather[i]
main = weather['main_weather']
if main == 'Clouds':
main = weather['description']
weather_img = Image.open(weather_dir + '/small_icons/' + weather_ids[main] + '.png')
min_img = self.textImage( "{0:.0f}".format(weather['min_temp']), small_font, r=0, g=0, b=255)
max_img = self.textImage( "{0:.0f}".format(weather['max_temp']), small_font, r=255, g=0, b=0)
img.paste(day_img, (x_offset +5, 9))
img.paste(weather_img, (x_offset +5, 16))
img.paste(min_img, (x_offset + 25, 10))
img.paste(max_img, (x_offset + 25, 20))
x_offset += 35
img1 = img.crop((64,0,x_offset ,32))
img1.save('display_images/weather.ppm')
return img0, img1
def getDailyWeatherImage(self):
all_settings = json.load(open('csv/daily_weather.json', 'r'))
if all_settings['title']:
title_img = self.openImage('feature_titles/forecast.png')
imgs = [title_img, self.blank]
else:
imgs = []
current_weathers = json.load(open('csv/current_weather.json', 'r'))
daily_weathers = json.load(open('csv/daily_weather.json', 'r'))
locations = list(daily_weathers['locations'].keys())
for i, location in enumerate(locations):
img = Image.new('RGB', (1000, 32))
current_weather = current_weathers['locations'][location]
daily_weather = daily_weathers['locations'][location]
small_font = ImageFont.load("./fonts/5x7.pil")
extra_small_font = ImageFont.load("./fonts/4x6.pil")
large_font = ImageFont.load("./fonts/10x20.pil")
location_img = self.textImage(location.upper(), extra_small_font, r = 255, g = 255, b = 0)
main = current_weather['main_weather']
if main == 'Clouds':
main = current_weather['description']
weather_ids = {'Clear': '01', 'few clouds': '02', 'scattered clouds': '03', 'broken clouds':'04', 'overcast clouds':'04', 'Drizzle':'09',
'Rain':'10', 'Thunderstorm':'11', 'Snow':'13', 'Mist': '50', 'Smoke': '50', 'Haze': '50', 'Dust': '50', 'Fog': '50',
'Sand': '50', 'Ash': '50', 'Squall': '50', 'Tornado': '50'}
weather_dir = './logos/weather_icons'
weather_img = Image.open(weather_dir + '/weather_type_icons/' + weather_ids[main] + '.png')
temp_img = self.textImage(str("{0:.0f}".format(current_weather['temp'])), large_font)
deg_img = self.textImage('o', small_font)
min_img = self.textImage( "{0:.0f}".format(current_weather['min_temp']), small_font, r=0, g=0, b=255)
max_img = self.textImage( "{0:.0f}".format(current_weather['max_temp']), small_font, r=255, g=0, b=0)
main = current_weather['main_weather']
main_img = self.textImage(main.upper(), small_font)
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
months =['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
month = months[int(datetime.now().strftime('%m'))-1]
date = str(int(datetime.now().strftime('%d')))
weekday = weekdays[datetime.today().weekday()]
date_img = self.textImage((month + ' ' + date + ',' + weekday).upper(), extra_small_font)
rain_img = Image.open(weather_dir + '/rain-chance.png')
rtext_img = self.textImage(str(int(current_weather['rain_chance']*100)) + '%', extra_small_font)
hum_img = Image.open(weather_dir + '/humidity.png')
htext_img = self.textImage(str(current_weather['humidity']) + '%', extra_small_font)
wind_img = Image.open(weather_dir + '/wind.png')
wtext_img = self.textImage(str(current_weather['wind_speed']) + 'm/s'.upper(), extra_small_font)
uv_img = Image.open(weather_dir + '/uv.png')
utext_img = self.textImage(str(round(current_weather['uv'], 1)) , small_font)
cloud_img = Image.open(weather_dir + '/clouds.png')
ctext_img = self.textImage(str(current_weather['clouds']) + '%', small_font)
wind_img = Image.open(weather_dir + '/wind.png')
wtext_img = self.textImage("{0:.0f}".format(current_weather['wind_speed']) + 'm/s'.upper(), small_font)
wdir_img = self.textImage(self.degreesToCompass(current_weather['wind_direction']), small_font)
vis_img = Image.open(weather_dir + '/visibility.png')
vtext_img = self.textImage(str(round(current_weather['visibility']/1000, 1)) + 'km'.upper(), small_font)
img.paste(location_img, (0,0))
x_offset = location_img.size[0]+2
img.paste(weather_img, (0,12))
img.paste(temp_img, (34,9))
img.paste(deg_img, (55, 8))
img.paste(min_img, (61, 10))
img.paste(main_img, (31, 26))
img.paste(max_img, (61, 18))
img.paste(date_img, (x_offset, 1))
x_offset += date_img.size[0]+2
img.paste(rain_img, (x_offset,0))
x_offset += rain_img.size[0]+2
img.paste(rtext_img, (x_offset, 1))
x_offset += rtext_img.size[0]+2
img.paste(hum_img, (x_offset, 0))
x_offset += hum_img.size[0]+2
img.paste(htext_img, (x_offset, 1))
x_offset+= htext_img.size[0]+2
img.paste(uv_img, ( x_offset, 0))
x_offset += uv_img.size[0]+2
img.paste(utext_img, (x_offset, 1))
x_offset += utext_img.size[0]+2
img.paste(cloud_img, (x_offset,0))
x_offset += cloud_img.size[0]+2
img.paste(ctext_img, (x_offset, 1))
x_offset += ctext_img.size[0]+2
img.paste(wind_img, (x_offset,0))
x_offset += wind_img.size[0]+2
img.paste(wtext_img, (x_offset, 1))
x_offset += wtext_img.size[0]+2
img.paste(wdir_img, (x_offset, 1))
x_offset+= wdir_img.size[0]+2
img.paste(vis_img, (x_offset,0))
x_offset+= vis_img.size[0]+2
img.paste(vtext_img, (x_offset, 1))
x_offset += vtext_img.size[0] +2
#img.paste(uv_img, ( 96, 0))
#img.paste(utext_img, (109, 0))
#img.paste(wind_img, (96,0))
#img.paste(wtext_img, (109,0))
#img.paste(date_img, (70, 0))
x_offset = 70
for i in range(1,len(daily_weather)-1):
weekday = weekdays[(datetime.today().weekday() + i)%7]
day_img = self.textImage( weekday.upper(), small_font)
weather = daily_weather[i]
main = weather['main_weather']
if main == 'Clouds':
main = weather['description']
weather_img = Image.open(weather_dir + '/small_icons/' + weather_ids[main] + '.png')
min_img = self.textImage( "{0:.0f}".format(weather['min_temp']), small_font, r=0, g=0, b=255)
max_img = self.textImage( "{0:.0f}".format(weather['max_temp']), small_font, r=255, g=0, b=0)
img.paste(day_img, (x_offset +5, 9))
img.paste(weather_img, (x_offset +5, 16))
img.paste(min_img, (x_offset + 25, 10))
img.paste(max_img, (x_offset + 25, 18))
x_offset += 40
img1 = img.crop((0,0,x_offset ,32))
imgs.append(img1)
imgs.append(self.blank)
# add the image text
return self.stitchImage(imgs)
def readSportsCSV(self, league):
team_info = {}
f = open('csv/sports/{}/team_info.csv'.format(league), 'r')
CSV = csv.reader(f)
next(CSV)
for row in CSV:
team_info[row[0]] = {}
team_info[row[0]]['id'] = row[1]
team_info[row[0]]['code'] = row[2]
team_info[row[0]]['logo'] = row[4]
team_info[row[0]]['colour'] = row[3]
return team_info
def displayDailyWeatherAlt(self):
img0, img1 = self.getDailyWeatherImageAlt()
#img = stock_ticker.getTodayWeatherImage()
while True:
self.setImage(img0, offset_x = 0, offset_y = 0)
image = img1
img_width, img_height = image.size
offset_x = 64
offset_y = 0
while offset_x > 64-img_width:
offset_x -= 1
self.setImage(image, offset_x = offset_x, offset_y = offset_y, min_x = 64, min_y = 9)
if offset_x + img_width < self.matrix.width: # if the image is ending
self.setImage(image, offset_x = offset_x + img_width, offset_y = offset_y, min_x = 64, min_y = 9)
try:
msg = getInput()
if msg == 'K':
self.resetMatrix()
return True
self.process_msg(msg)
except KeyboardInterrupt:
sys.stdout.flush()
pass
time.sleep(self.delay*1.1)
def getUserImage(self):
title_img = self.openImage('feature_titles/images.png')
f = open('csv/image_settings.json', 'r')
all_settings = json.load(f)
f.close()
if all_settings['title']:
imgs = [title_img]
else:
imgs = []
for image in all_settings['images']:
image = self.openImage(os.path.join(os.path.dirname(os.path.abspath(__file__)), image))
return self.stitchImage(imgs)
def getUserGIF(self):
title_img = self.openImage('feature_titles/gifs.png')
gif = Image.open(os.path.join(os.path.dirname(os.path.abspath(__file__)), all_settings['images'][0]))
f = open('csv/GIF_settings.json', 'r')
all_settings = json.load(f)
f.close()
frames = []
for i, frame in enumerate(ImageSequence.Iterator(gif)):
if all_settings['title']:
f = self.stitchImage([title_img, frame])
else:
f = self.stitchImage([frame])
frames.append(f)
frames[0].save('./display_images/Custom GIFs.gif', save_all=True, append_images=frames[1:], loop=0, optimize = False)
return None
def displayStocks(self):
self.scrollImageTransition(['final.ppm', 'final.ppm'], offset_x = 0, offset_y = 0)
def displayProfessional(self):
forex = self.getForexProfessional()
crypto = self.getCryptoProfessional()
news = self.getNewsProfessional()
stock = self.getStockProfessional()
weather = self.getTodayWeatherProfessional()
x_offset = 0
news.paste(weather, (x_offset, 16))
x_offset += weather.size[0]
news.paste(crypto, (x_offset, 16))
x_offset += crypto.size[0]
news.paste(stock, (x_offset, 16))
x_offset += stock.size[0]
news.paste(forex, (x_offset, 16))
x_offset += forex.size[0]
self.double_buffer = self.matrix.CreateFrameCanvas()
while True:
kill = stock_ticker.scrollImage(news, offset_x = 128)
if kill:
break
def scrollProfessionalAnimated(self, options, animation = 'on'):
# scrolls trhough all functions with animation. Updates functions and remakes images when each function not being dispplayed
print(options[0:2])
self.updateMultiple(options[0:2])
kill = False
i1 = 0 # keep track of which image we are displaying
i2 = 1 # keep track of which image we are displaying
self.double_buffer = self.matrix.CreateFrameCanvas()
update_process = Process(target = self.updateMultiple, args = ([options[(max(i1,i2)+1) % len(options)]],))
update_process.start()
image1 = self.openImage('./display_images/' + options[i1 % len(options)] +'.ppm')
image1 = image1.convert('RGB')
print(options[i2 % len(options)])
image2 = self.openImage('./display_images/' + options[i2 % len(options)] +'.ppm')
image2 = image2.convert('RGB')
offset_x1 = 0
offset_x2 = 0
offset_y1 = -16
offset_y2 = 32
frame_skip = int((1/15)/self.delay) #controls how fast gifs run
self.frame = 0
img_width1, img_height1 = image1.size
img_width2, img_height2 = image2.size
kill = False
delay_t1 = 0.02
delay_t2 = 0.025
update_t1 = time.time()
update_t2 = time.time()
while True:
if offset_x1 < -(img_width1+1):
i1 = max(i1, i2) + 1
offset_x1 = 0
offset_y1 = -16
update_process.join()
update_process = Process(target = self.updateMultiple, args = ([options[(max(i1,i2)+1) % len(options)]],))
update_process.start()
image1 = self.openImage('./display_images/' + options[i1 % len(options)] +'.ppm')
image1 = image1.convert('RGB')
img_width1, img_height1 = image1.size
if offset_x2 < -(img_width2+1):
i2 = max(i1, i2) + 1
offset_x2 = 0
offset_y2 = 32
update_process.join()
update_process = Process(target = self.updateMultiple, args = ([options[(max(i1,i2)+1) % len(options)]],))
update_process.start()
image2 = self.openImage('./display_images/' + options[i2 % len(options)] +'.ppm')
image2 = image2.convert('RGB')
img_width2, img_height2 = image2.size
if time.time() - update_t1 > delay_t1:
update_t1 = time.time()
if offset_y1 < 0:
offset_y1+=1
else:
offset_x1 -= 1
if time.time() - update_t2 > delay_t2:
update_t2 = time.time()
if offset_y2 > 16:
offset_y2-=1
else:
offset_x2 -= 1
if kill: break
#image = image.convert('RGB')
self.double_buffer.SetImage(image1, offset_x1, offset_y1)
self.double_buffer.SetImage(image2, offset_x2, offset_y2)
buff = 0
# remove the ppixels behind the image, to stop trailing
self.double_buffer = self.matrix.SwapOnVSync(self.double_buffer)
for y in range(16):
self.matrix.SetPixel(offset_x1 + img_width1 +1 , y , 0,0,0)
self.matrix.SetPixel(offset_x1 + img_width1 , y , 0,0,0)
for y in range(16,32):
self.matrix.SetPixel(offset_x2 + img_width2 +1 , y , 0,0,0)
self.matrix.SetPixel(offset_x2 + img_width2 , y , 0,0,0)
kill = self.checkKilled()
if kill: break
if kill: break
def process_msg(self, msg):
if msg == 'S': # stocks
self.scrollFunctionsAnimated(['stocks', 'stocks'], animation = 'traditional')
elif msg == 'C': # crypto
self.scrollFunctionsAnimated(['crypto', 'crypto'], animation = 'traditional')
elif msg == 'F': # forex
self.scrollFunctionsAnimated(['forex', 'forex'], animation = 'traditional')
elif msg == 'N': #news
self.scrollFunctionsAnimated(['news', 'news'], animation = 'traditional')
# speed settings
elif msg == 's':
self.delay = 0.03
elif msg == 'm':
self.delay = 0.02
elif msg == 'f':
self.delay = 0.01
elif msg in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']: # birghtness ettings
self.brightness = min(1.0, float(msg)/10 + 0.1)
elif msg == 'T':# text
self.scrollFunctionsAnimated(['text', 'text'], animation = 'traditional')
elif msg == 'I': # image
self.scrollFunctionsAnimated(['display_image', 'display_image'], animation = 'traditional')
elif msg == 'G': # gif
self.scrollFunctionsAnimated(['display_gif', 'display_gif'], animation = 'traditional')
elif msg == 'W': # weather
self.scrollFunctionsAnimated(['today_weather', 'today_weather'], animation = 'traditional')
elif msg == 'D': # daily weather
self.scrollFunctionsAnimated(['daily_weather', 'daily_weather'],animation = 'traditional')
elif msg == 'P': # past league
self.scrollFunctionsAnimated(['past_games', 'past_games'],animation = 'traditional')
elif msg == 'l': # future league
self.scrollFunctionsAnimated(['future_games', 'future_games'],animation = 'traditional')
elif msg == 'L': # live game
self.scrollFunctionsAnimated(['live_games', 'live_games'],animation = 'traditional')
elif msg == 't': #legue tble
self.scrollFunctionsAnimated(['league_table', 'league_table'],animation = 'traditional')
elif msg == 'A': #everything
#userSettings = ['display_gif', 'text', 'display_image', 'stocks', 'crypto', 'forex', 'today_weather', 'daily_weather', 'league_table', 'league_games', 'news'] # these wil be read from csv, just for demo
#userSettings = [ 'daily_weather']
#userSettings = ['crypto', 'stocks'] # these wil be read from csv, just for demo
#userSettings = [ 'display_image', 'news'] # these wil be read from csv, just for demo
userSettings = json.load(open('csv/display_settings.json'))
if 'Prof' in userSettings[0]: #if professional display
self.scrollProfessionalAnimated(userSettings)
else:
self.scrollFunctionsAnimated(userSettings, animation = 'down')
elif msg == 'b':
userSettings = json.load(open('csv/display_settings.json'))
self.scrollProfessionalAnimated(userSettings)
#self.displayProfessional()
elif msg == '+':
stock_ticker.scrollMultiple()
elif msg == 'K': # kill
self.resetMatrix()
if __name__ == '__main__':
with open('log.txt', "w") as log:
try:
stock_ticker = StockTicker()
start_image = Image.open('./logos/startup_logo.png')
stock_ticker.setImage(start_image)
time.sleep(1)
stock_ticker.resetMatrix()
#
#stock_ticker.process_msg('G')
#stock_ticker.process_msg('f')
#stock_ticker.process_msg('W')
while True:
msg = getInput()
stock_ticker.process_msg(msg)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
log.write(str(e))
log.write('. file: ' + fname)
log.write('. line: ' + str(exc_tb.tb_lineno))
log.write('. type: ' + str(exc_type))
log.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
raise(e)