Update configuration.py
This commit is contained in:
		| @@ -9,8 +9,12 @@ from PIL import Image, ImageDraw, ImageFont | |||||||
| from urllib.request import urlopen | from urllib.request import urlopen | ||||||
| from settings import language | from settings import language | ||||||
| from pytz import timezone | from pytz import timezone | ||||||
| import numpy as np |  | ||||||
| import os | 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)""" | """Set the display height and width (in pixels)""" | ||||||
| display_height, display_width = 640, 384 | display_height, display_width = 640, 384 | ||||||
| @@ -18,11 +22,12 @@ display_height, display_width = 640, 384 | |||||||
| """Create 3 sections of the display, based on percentage""" | """Create 3 sections of the display, based on percentage""" | ||||||
| top_section_width = middle_section_width = bottom_section_width = display_width | 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) | middle_section_height = int(display_height*0.65) | ||||||
| bottom_section_height = int(display_height - middle_section_height - | bottom_section_height = int(display_height - middle_section_height - | ||||||
|                             top_section_height) |                             top_section_height) | ||||||
|  |  | ||||||
|  | """Find out the y-axis position of each section""" | ||||||
| top_section_offset = 0 | top_section_offset = 0 | ||||||
| middle_section_offset = top_section_height | middle_section_offset = top_section_height | ||||||
| bottom_section_offset = display_height - bottom_section_height | bottom_section_offset = display_height - bottom_section_height | ||||||
| @@ -34,6 +39,8 @@ if path != "" and path[-1] != "/": | |||||||
| while not path.endswith('/Inky-Calendar/'): | while not path.endswith('/Inky-Calendar/'): | ||||||
|   path = ''.join(list(path)[:-1]) |   path = ''.join(list(path)[:-1]) | ||||||
|  |  | ||||||
|  | """Select path for saving temporary image files""" | ||||||
|  | image_path = path + 'images/' | ||||||
|  |  | ||||||
| """Fonts handling""" | """Fonts handling""" | ||||||
| fontpath = path+'fonts/' | fontpath = path+'fonts/' | ||||||
| @@ -41,12 +48,12 @@ NotoSansCJK = fontpath+'NotoSansCJK/NotoSansCJKsc-' | |||||||
| NotoSans = fontpath+'NotoSans/NotoSans-SemiCondensed' | NotoSans = fontpath+'NotoSans/NotoSans-SemiCondensed' | ||||||
| weatherfont = fontpath+'WeatherFont/weathericons-regular-webfont.ttf' | weatherfont = fontpath+'WeatherFont/weathericons-regular-webfont.ttf' | ||||||
|  |  | ||||||
|  | """Automatically select correct fonts to support set language""" | ||||||
| if language in ['ja','zh','zh_tw','ko']: | if language in ['ja','zh','zh_tw','ko']: | ||||||
|   default = ImageFont.truetype(NotoSansCJK+'Light.otf', 18) |   default = ImageFont.truetype(NotoSansCJK+'Light.otf', 18) | ||||||
|   semi = ImageFont.truetype(NotoSansCJK+'DemiLight.otf', 18) |   semi = ImageFont.truetype(NotoSansCJK+'DemiLight.otf', 18) | ||||||
|   bold = ImageFont.truetype(NotoSansCJK+'Regular.otf', 18) |   bold = ImageFont.truetype(NotoSansCJK+'Regular.otf', 18) | ||||||
|   month_font = ImageFont.truetype(NotoSansCJK+'DemiLight.otf', 40) |   month_font = ImageFont.truetype(NotoSansCJK+'DemiLight.otf', 40) | ||||||
|  |  | ||||||
| else: | else: | ||||||
|   default = ImageFont.truetype(NotoSans+'Light.ttf', 18) |   default = ImageFont.truetype(NotoSans+'Light.ttf', 18) | ||||||
|   semi = ImageFont.truetype(NotoSans+'.ttf', 18) |   semi = ImageFont.truetype(NotoSans+'.ttf', 18) | ||||||
| @@ -55,39 +62,46 @@ else: | |||||||
|  |  | ||||||
| w_font = ImageFont.truetype(weatherfont, 10) | w_font = ImageFont.truetype(weatherfont, 10) | ||||||
|  |  | ||||||
| x_padding = int((display_width % 10) // 2) | """Create image with given parameters""" | ||||||
| line_height = default.getsize('hg')[1] | image = Image.new('RGB', (display_width, display_height), background_colour) | ||||||
| line_width = display_width- x_padding |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | """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') |   if autofit == True or fill_width != 1.0 or fill_height != 0.8: | ||||||
| #def main(): |     size = 8 | ||||||
| def write_text(box_width, box_height, text, tuple, |     font = ImageFont.truetype(font.path, size) | ||||||
|   font=default, alignment='middle', adapt_fontsize = False): |  | ||||||
|     text_width, text_height = font.getsize(text) |     text_width, text_height = font.getsize(text) | ||||||
|   if adapt_fontsize == True: |     while text_width < int(space_width * fill_width) and text_height < int(space_height * fill_height): | ||||||
|     size = 10 |  | ||||||
|     while text_width < box_width and text_height < box_height: |  | ||||||
|       size += 1 |       size += 1 | ||||||
|       font = ImageFont.truetype(font.path, size) |       font = ImageFont.truetype(font.path, size) | ||||||
|       text_width, text_height = font.getsize(text) |       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=text[0:-1] | ||||||
|     text_width, text_height = font.getsize(text) |     text_width, text_height = font.getsize(text) | ||||||
|   if alignment is "" or "middle" or None: |   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': |   if alignment is 'left': | ||||||
|     x = 0 |     x = 0 | ||||||
|   y = int((box_height / 2) - (text_height / 1.7)) |   if font != w_font: | ||||||
|   space = Image.new('RGB', (box_width, box_height), color='white') |     y = int((space_height / 2) - (text_height / 1.7)) | ||||||
|   ImageDraw.Draw(space).text((x, y), text, fill='black', font=font) |   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) |   image.paste(space, tuple) | ||||||
|  |  | ||||||
|  |  | ||||||
| """Custom function to display longer text into multiple lines (wrapping)""" | """Custom function to display longer text into multiple lines (wrapping)""" | ||||||
| def text_wrap(text, font=default, line_width = display_width): | def text_wrap(text, font=default, line_width = display_width): | ||||||
|   counter, padding = 0, 60 |   counter, padding = 0, 40 | ||||||
|   lines = [] |   lines = [] | ||||||
|   if font.getsize(text)[0] < line_width: |   if font.getsize(text)[0] < line_width: | ||||||
|     lines.append(text) |     lines.append(text) | ||||||
| @@ -102,9 +116,7 @@ def text_wrap(text, font=default, line_width = display_width): | |||||||
|   return lines |   return lines | ||||||
|  |  | ||||||
|  |  | ||||||
|  | """Function to check internet connectivity""" | ||||||
|  |  | ||||||
| """Check if internet is available by trying to reach google""" |  | ||||||
| def internet_available(): | def internet_available(): | ||||||
|   try: |   try: | ||||||
|     urlopen('https://google.com',timeout=5) |     urlopen('https://google.com',timeout=5) | ||||||
| @@ -112,7 +124,7 @@ def internet_available(): | |||||||
|   except URLError as err: |   except URLError as err: | ||||||
|     return False |     return False | ||||||
|  |  | ||||||
| '''Get system timezone and set timezone accordingly''' | """Function to get the system timezone""" | ||||||
| def get_tz(): | def get_tz(): | ||||||
|   with open('/etc/timezone','r') as file: |   with open('/etc/timezone','r') as file: | ||||||
|     lines = file.readlines() |     lines = file.readlines() | ||||||
| @@ -120,7 +132,6 @@ def get_tz(): | |||||||
|     local_tz = timezone(system_tz) |     local_tz = timezone(system_tz) | ||||||
|   return local_tz |   return local_tz | ||||||
|  |  | ||||||
|  |  | ||||||
| def fix_ical(ical_url): | def fix_ical(ical_url): | ||||||
|   ical = str(urlopen(ical_url).read().decode()) |   ical = str(urlopen(ical_url).read().decode()) | ||||||
|   beginAlarmIndex = 0 |   beginAlarmIndex = 0 | ||||||
| @@ -131,41 +142,9 @@ def fix_ical(ical_url): | |||||||
|       ical = ical[:beginAlarmIndex] + ical[endAlarmIndex+12:] |       ical = ical[:beginAlarmIndex] + ical[endAlarmIndex+12:] | ||||||
|   return ical |   return ical | ||||||
|  |  | ||||||
|  | """Function to clear images folder""" | ||||||
| def reduce_colours(image): | def image_cleanup(): | ||||||
|   buffer = np.array(image) |   print('Cleanup of previous images...', end = '') | ||||||
|   r,g,b = buffer[:,:,0], buffer[:,:,1], buffer[:,:,2] |   for temp_files in glob(image_path+'*'): | ||||||
|  |       os.remove(temp_files) | ||||||
|   if display_colours == "bwr": |   print('Done') | ||||||
|       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() |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user