From da4535af56571a0a5e2959a89421f4ed2c07226d Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 9 Jan 2023 18:20:11 +0800 Subject: [PATCH] Update stockTicker.py --- stockTicker.py | 180 +++---------------------------------------------- 1 file changed, 8 insertions(+), 172 deletions(-) diff --git a/stockTicker.py b/stockTicker.py index 95f0f9c..413d383 100755 --- a/stockTicker.py +++ b/stockTicker.py @@ -12,7 +12,7 @@ import threading from PIL import Image, ImageDraw, ImageFont, ImageSequence Image.init() - +import pytz import time import csv import requests @@ -1974,14 +1974,13 @@ class StockTicker(): #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]) - - - + img = Image.new('RGB', (headline_img.size[0]+ source_img.size[0] + logo.size[0] +22, 32)) + img.paste(headline_img, (source_img.size[0]+logo.size[0] + 10, 3)) + img.paste(source_img, (logo.size[0]+2,3)) + img.paste(logo, (0,0)) + + + image_list.append(img) image_list.append(blank) @@ -2001,169 +2000,6 @@ class StockTicker(): 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' - - - - - f = open(filepath, 'r') - all_settings = json.load(f) - f.close() - - 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: - try: - x_offset = 0 - img = Image.new('RGB', (10000, 32)) - league_info = leagues_info[league] - - - try: - league_logo = Image.open('logos/sports/league_logos/{}.png'.format(league)).convert('RGB') - img.paste(league_logo, (x_offset,0)) - x_offset += league_logo.size[0] +self.blank.size[0] - except: - pass - - 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 - - for match in league_info: - strHomeTeam = match['home_team'] - strAwayTeam = match['away_team'] - - if time != 'future': - intHomeScore = str(match['home_score']) - intAwayScore = str(match['away_score']) - - dateEvent = match['date'].replace('-', '.') - - - try: - - home_logo = Image.open('logos/sports/{}/{}'.format(league, sports_info[strHomeTeam]['logo'])) - - except Exception as e: - home_logo = self.textImage(strHomeTeam.replace(' ', '\n'), small_font, r = 255, g = 255, b = 255) - - - try: - - away_logo = Image.open('logos/sports/{}/{}'.format(league, sports_info[strAwayTeam]['logo'])) - - except Exception as e: - away_logo = self.textImage(strAwayTeam.replace(' ', '\n'), small_font, r = 255, g = 255, b = 255) - - - date_timage = self.textImage(dateEvent, 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[strHomeTeam]['colour'].replace(' ', '')) - a_colour = mcolors.to_rgb(sports_info[strAwayTeam]['colour'].replace(' ', '')) - - - hc_timage = self.textImage(sports_info[strHomeTeam]['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[strAwayTeam]['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(intHomeScore + '-' + intAwayScore, 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[strHomeTeam]['colour'].replace(' ', '')) - a_colour = mcolors.to_rgb(sports_info[strAwayTeam]['colour'].replace(' ', '')) - - hc_timage = self.textImage(sports_info[strHomeTeam]['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[strAwayTeam]['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)) - imgs.append(img) - except Exception as e: - pass -# logf = open('log.txt', "a") -# exc_type, exc_obj, exc_tb = sys.exc_info() -# fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] -# logf.write(str(e)) -# logf.write('. file: ' + fname) -# logf.write('. line: ' + str(exc_tb.tb_lineno)) -# logf.write('. type: ' + str(exc_type)) -# logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))) -# logf.close() - - - - return self.stitchImage(imgs) - - def getLeagueImage(self, league=False, time = 'past'): if time in ['past', 'live']: