From bb3264ebf89e2cac2eca72e02e90367a5b3930b7 Mon Sep 17 00:00:00 2001 From: Ace Date: Mon, 25 Nov 2019 21:26:56 +0100 Subject: [PATCH] Update configuration.py --- Inky-Calendar/modules/configuration.py | 107 ++++++++++--------------- 1 file changed, 44 insertions(+), 63 deletions(-) diff --git a/Inky-Calendar/modules/configuration.py b/Inky-Calendar/modules/configuration.py index bb21264..ca88f0b 100644 --- a/Inky-Calendar/modules/configuration.py +++ b/Inky-Calendar/modules/configuration.py @@ -1,3 +1,5 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- """ Advanced configuration options for Inky-Calendar software. Contains some useful functions for correctly rendering text, @@ -9,8 +11,12 @@ from PIL import Image, ImageDraw, ImageFont from urllib.request import urlopen from settings import language from pytz import timezone -import numpy as np import os +from glob import glob + +"""Set the image background colour and text colour""" +background_colour = 'white' +text_colour = 'black' """Set the display height and width (in pixels)""" display_height, display_width = 640, 384 @@ -18,11 +24,12 @@ display_height, display_width = 640, 384 """Create 3 sections of the display, based on percentage""" top_section_width = middle_section_width = bottom_section_width = display_width -top_section_height = int(display_height*0.10) +top_section_height = int(display_height*0.11) middle_section_height = int(display_height*0.65) bottom_section_height = int(display_height - middle_section_height - top_section_height) +"""Find out the y-axis position of each section""" top_section_offset = 0 middle_section_offset = top_section_height bottom_section_offset = display_height - bottom_section_height @@ -34,6 +41,8 @@ if path != "" and path[-1] != "/": while not path.endswith('/Inky-Calendar/'): path = ''.join(list(path)[:-1]) +"""Select path for saving temporary image files""" +image_path = path + 'images/' """Fonts handling""" fontpath = path+'fonts/' @@ -41,12 +50,12 @@ NotoSansCJK = fontpath+'NotoSansCJK/NotoSansCJKsc-' NotoSans = fontpath+'NotoSans/NotoSans-SemiCondensed' weatherfont = fontpath+'WeatherFont/weathericons-regular-webfont.ttf' +"""Automatically select correct fonts to support set language""" if language in ['ja','zh','zh_tw','ko']: default = ImageFont.truetype(NotoSansCJK+'Light.otf', 18) semi = ImageFont.truetype(NotoSansCJK+'DemiLight.otf', 18) bold = ImageFont.truetype(NotoSansCJK+'Regular.otf', 18) month_font = ImageFont.truetype(NotoSansCJK+'DemiLight.otf', 40) - else: default = ImageFont.truetype(NotoSans+'Light.ttf', 18) semi = ImageFont.truetype(NotoSans+'.ttf', 18) @@ -55,39 +64,46 @@ else: w_font = ImageFont.truetype(weatherfont, 10) -x_padding = int((display_width % 10) // 2) -line_height = default.getsize('hg')[1] -line_width = display_width- x_padding +"""Create image with given parameters""" +image = Image.new('RGB', (display_width, display_height), background_colour) +"""Custom function to add text on an image""" +def write_text(space_width, space_height, text, tuple, + font=default, alignment='middle', autofit = False, fill_width = 1.0, + fill_height = 0.8): -image = Image.new('RGB', (display_width, display_height), 'white') -#def main(): -def write_text(box_width, box_height, text, tuple, - font=default, alignment='middle', adapt_fontsize = False): - text_width, text_height = font.getsize(text) - if adapt_fontsize == True: - size = 10 - while text_width < box_width and text_height < box_height: + if autofit == True or fill_width != 1.0 or fill_height != 0.8: + size = 8 + font = ImageFont.truetype(font.path, size) + text_width, text_height = font.getsize(text) + while text_width < int(space_width * fill_width) and text_height < int(space_height * fill_height): size += 1 font = ImageFont.truetype(font.path, size) text_width, text_height = font.getsize(text) - while (text_width, text_height) > (box_width, box_height): + text_width, text_height = font.getsize(text) + + while (text_width, text_height) > (space_width, space_height): text=text[0:-1] text_width, text_height = font.getsize(text) if alignment is "" or "middle" or None: - x = int((box_width / 2) - (text_width / 2)) + x = int((space_width / 2) - (text_width / 2)) if alignment is 'left': x = 0 - y = int((box_height / 2) - (text_height / 1.7)) - space = Image.new('RGB', (box_width, box_height), color='white') - ImageDraw.Draw(space).text((x, y), text, fill='black', font=font) + if font != w_font: + y = int((space_height / 2) - (text_height / 1.7)) + else: + y = y = int((space_height / 2) - (text_height / 2)) + + space = Image.new('RGB', (space_width, space_height), color=background_colour) + ImageDraw.Draw(space).text((x, y), text, fill=text_colour, font=font) image.paste(space, tuple) + """Custom function to display longer text into multiple lines (wrapping)""" def text_wrap(text, font=default, line_width = display_width): - counter, padding = 0, 60 + counter, padding = 0, 40 lines = [] if font.getsize(text)[0] < line_width: lines.append(text) @@ -102,9 +118,7 @@ def text_wrap(text, font=default, line_width = display_width): return lines - - -"""Check if internet is available by trying to reach google""" +"""Function to check internet connectivity""" def internet_available(): try: urlopen('https://google.com',timeout=5) @@ -112,7 +126,7 @@ def internet_available(): except URLError as err: return False -'''Get system timezone and set timezone accordingly''' +"""Function to get the system timezone""" def get_tz(): with open('/etc/timezone','r') as file: lines = file.readlines() @@ -120,7 +134,6 @@ def get_tz(): local_tz = timezone(system_tz) return local_tz - def fix_ical(ical_url): ical = str(urlopen(ical_url).read().decode()) beginAlarmIndex = 0 @@ -131,41 +144,9 @@ def fix_ical(ical_url): ical = ical[:beginAlarmIndex] + ical[endAlarmIndex+12:] return ical - -def reduce_colours(image): - buffer = np.array(image) - r,g,b = buffer[:,:,0], buffer[:,:,1], buffer[:,:,2] - - if display_colours == "bwr": - buffer[np.logical_and(r > 245, g > 245)] = [255,255,255] #white - buffer[np.logical_and(r > 245, g < 245)] = [255,0,0] #red - buffer[np.logical_and(r != 255, r == g )] = [0,0,0] #black - else: - buffer[np.logical_and(r > 245, g > 245)] = [255,255,255] #white - buffer[g < 255] = [0,0,0] #black - - image = Image.fromarray(buffer).rotate(270, expand=True) - return image - -def calibrate(cycles): - """Function for Calibration""" - import e_paper_drivers - epd = e_paper_drivers.EPD() - print('----------Started calibration of E-Paper display----------') - - for i in range(cycles): - epd.init() - print('Calibrating black...') - epd.display_frame(epd.get_frame_buffer(black)) - if display_colours == "bwr": - print('calibrating red...') - epd.display_frame(epd.get_frame_buffer(red)) - print('Calibrating white...') - epd.display_frame(epd.get_frame_buffer(white)) - epd.sleep() - print('Cycle {0} of {1} complete'.format(i, cycle)) - print('-----------Calibration complete----------') - -#if __name__ == '__main__': - #main() - +"""Function to clear images folder""" +def image_cleanup(): + print('Cleanup of previous images...', end = '') + for temp_files in glob(image_path+'*'): + os.remove(temp_files) + print('Done')