minor improvements
inkycal_rss: fix format of saved images inkycal_weather: * drop support for wind direction (api does not always provide this data) * Add borders around each sub-section
This commit is contained in:
		| @@ -139,6 +139,8 @@ def write(image, xy, box_size, text, font=None, **kwargs): | |||||||
|   draw  = ImageDraw.Draw(image) |   draw  = ImageDraw.Draw(image) | ||||||
|   space = Image.new('RGBA', (box_width, box_height)) |   space = Image.new('RGBA', (box_width, box_height)) | ||||||
|   ImageDraw.Draw(space).text((x, y), text, fill=colour, font=font) |   ImageDraw.Draw(space).text((x, y), text, fill=colour, font=font) | ||||||
|  |   # Uncomment following two lines, comment out above two lines to show | ||||||
|  |   # red text-box with white text (debugging purposes) | ||||||
| ##  space = Image.new('RGBA', (box_width, box_height), color= 'red') | ##  space = Image.new('RGBA', (box_width, box_height), color= 'red') | ||||||
| ##  ImageDraw.Draw(space).text((x, y), text, fill='white', font=font) | ##  ImageDraw.Draw(space).text((x, y), text, fill='white', font=font) | ||||||
|  |  | ||||||
| @@ -179,22 +181,26 @@ def internet_available(): | |||||||
|     return False |     return False | ||||||
|  |  | ||||||
|  |  | ||||||
| def draw_square(image, xy, size, radius=5, thickness=2): | def draw_border(image, xy, size, radius=5, thickness=1, shrinkage=(0.1,0.1)): | ||||||
|   """Draws a square with round corners at (x,y) |   """Draws a border with round corners at (x,y) | ||||||
|   xy = position e.g: (5,10) |   xy = position e.g: (5,10) | ||||||
|   size = size of square (width, height) |   size = size of border (width, height), radius: corner radius | ||||||
|   radius: corner radius |  | ||||||
|   thickness = border thickness |   thickness = border thickness | ||||||
|  |   shrinkage = shrink and center border by given percentage:(width_%, height_%) | ||||||
|   """ |   """ | ||||||
|    |    | ||||||
|   x, y, diameter = xy[0], xy[1], radius*2 |  | ||||||
|   colour='black' |   colour='black' | ||||||
|   width, height = size |   # size from function paramter | ||||||
|   lenght = width - diameter |   width, height = size[0]*(1-shrinkage[0]), size[1]*(1-shrinkage[1]) | ||||||
|  |   offset_x, offset_y = int((size[0] - width)/2), int((size[1]- height)/2) | ||||||
|  |    | ||||||
|  |   x, y, diameter = xy[0]+offset_x, xy[1]+offset_y, radius*2 | ||||||
|  |   # lenght of rectangle size | ||||||
|  |   a,b = (width - diameter), (height-diameter) | ||||||
|  |  | ||||||
|   # Set coordinates for round square |   # Set coordinates for round square | ||||||
|   p1, p2 = (x+radius, y), (x+radius+lenght, y) |   p1, p2 = (x+radius, y), (x+radius+a, y) | ||||||
|   p3, p4 = (x+width, y+radius), (x+width, y+radius+lenght) |   p3, p4 = (x+width, y+radius), (x+width, y+radius+b) | ||||||
|   p5, p6 = (p2[0], y+height), (p1[0], y+height) |   p5, p6 = (p2[0], y+height), (p1[0], y+height) | ||||||
|   p7, p8  = (x, p4[1]), (x,p3[1]) |   p7, p8  = (x, p4[1]), (x,p3[1]) | ||||||
|   c1, c2 = (x,y), (x+diameter, y+diameter) |   c1, c2 = (x,y), (x+diameter, y+diameter) | ||||||
| @@ -215,45 +221,6 @@ def draw_square(image, xy, size, radius=5, thickness=2): | |||||||
|   draw.arc(  (c7, c8) , 90, 180, fill=colour, width=thickness) |   draw.arc(  (c7, c8) , 90, 180, fill=colour, width=thickness) | ||||||
|  |  | ||||||
|  |  | ||||||
| ##"""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, colour = text_colour, rotation = None): |  | ||||||
| ##  """tuple refers to (x,y) position on display""" |  | ||||||
| ##  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)[0], font.getsize('hg')[1] |  | ||||||
| ##    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)[0], font.getsize('hg')[1] |  | ||||||
| ## |  | ||||||
| ##  text_width, text_height = font.getsize(text)[0], font.getsize('hg')[1] |  | ||||||
| ## |  | ||||||
| ##  while (text_width, text_height) > (space_width, space_height): |  | ||||||
| ##    text=text[0:-1] |  | ||||||
| ##    text_width, text_height = font.getsize(text)[0], font.getsize('hg')[1] |  | ||||||
| ##  if alignment is "" or "middle" or None: |  | ||||||
| ##    x = int((space_width / 2) - (text_width / 2)) |  | ||||||
| ##  if alignment is 'left': |  | ||||||
| ##    x = 0 |  | ||||||
| ##  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('RGBA', (space_width, space_height)) |  | ||||||
| ##  ImageDraw.Draw(space).text((x, y), text, fill=colour, font=font) |  | ||||||
| ##  if rotation != None: |  | ||||||
| ##    space.rotate(rotation, expand = True) |  | ||||||
| ## |  | ||||||
| ##  if colour == 'black' or 'white': |  | ||||||
| ##    image.paste(space, tuple, space) |  | ||||||
| ##  else: |  | ||||||
| ##    image_col.paste(space, tuple, space) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| """Not required anymore?""" | """Not required anymore?""" | ||||||
| ##def clear_image(section, colour = background_colour): | ##def clear_image(section, colour = background_colour): | ||||||
| ##  """Clear the image""" | ##  """Clear the image""" | ||||||
|   | |||||||
| @@ -12,17 +12,17 @@ try: | |||||||
|   import feedparser |   import feedparser | ||||||
| except ImportError: | except ImportError: | ||||||
|   print('feedparser is not installed! Please install with:') |   print('feedparser is not installed! Please install with:') | ||||||
|   print('pipe install feedparser') |   print('pip3 install feedparser') | ||||||
|  |  | ||||||
|  |  | ||||||
| # Debug Data | # Debug Data (not for production use!) | ||||||
| size = (384, 160) | size = (384, 160) | ||||||
| config = {'rss_urls': ['http://feeds.bbci.co.uk/news/world/rss.xml#']} | config = {'rss_urls': ['http://feeds.bbci.co.uk/news/world/rss.xml#']} | ||||||
|  |  | ||||||
|  |  | ||||||
| class rss: | class rss: | ||||||
|   """RSS class |   """RSS class | ||||||
|   parses rss feeds from given urls and writes them on the image |   parses rss feeds from given urls | ||||||
|   """ |   """ | ||||||
|  |  | ||||||
|   logger = logging.getLogger(__name__) |   logger = logging.getLogger(__name__) | ||||||
| @@ -142,8 +142,8 @@ class rss: | |||||||
|       write(im_black, (0,0), (im_width, im_height), str(e), font = self.font) |       write(im_black, (0,0), (im_width, im_height), str(e), font = self.font) | ||||||
|  |  | ||||||
|     # Save image of black and colour channel in image-folder |     # Save image of black and colour channel in image-folder | ||||||
|     im_black.save(images+self.name+'.png') |     im_black.save(images+self.name+'.png', 'PNG') | ||||||
|     im_colour.save(images+self.name+'_colour.png') |     im_colour.save(images+self.name+'_colour.png', 'PNG') | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|   print('running {0} in standalone mode'.format( |   print('running {0} in standalone mode'.format( | ||||||
|   | |||||||
| @@ -6,15 +6,26 @@ Copyright by aceisace | |||||||
| """ | """ | ||||||
|  |  | ||||||
| from inkycal.custom import * | from inkycal.custom import * | ||||||
| import pyowm |  | ||||||
| import math, decimal | import math, decimal | ||||||
| import arrow | import arrow | ||||||
| from locale import getdefaultlocale as sys_locale | from locale import getdefaultlocale as sys_locale | ||||||
|  |  | ||||||
| config = {'api_key': 'top-secret', 'location': 'Stuttgart, DE'} | try: | ||||||
|  |   import pyowm | ||||||
|  | except ImportError: | ||||||
|  |   print('pyowm is not installed! Please install with:') | ||||||
|  |   print('pip3 install pyowm') | ||||||
|  |  | ||||||
|  |  | ||||||
|  | # Debug Data (not for production use!) | ||||||
|  | config = {'api_key': 'secret', 'location': 'Stuttgart, DE'} | ||||||
| size = (384,80) | size = (384,80) | ||||||
|  |  | ||||||
| class weather: | class weather: | ||||||
|  |   """weather class | ||||||
|  |   parses weather details from openweathermap | ||||||
|  |   """ | ||||||
|  |  | ||||||
|   logger = logging.getLogger(__name__) |   logger = logging.getLogger(__name__) | ||||||
|   logging.basicConfig(level=logging.INFO) |   logging.basicConfig(level=logging.INFO) | ||||||
|  |  | ||||||
| @@ -34,14 +45,12 @@ class weather: | |||||||
|     # Weather-specfic options |     # Weather-specfic options | ||||||
|     self.owm = pyowm.OWM(config['api_key']) |     self.owm = pyowm.OWM(config['api_key']) | ||||||
|     self.units = 'metric' # metric # imperial |     self.units = 'metric' # metric # imperial | ||||||
|     self.hour_format = '12' # 12 #24 |     self.hour_format = '24' # 12 #24 | ||||||
|     self.timezone = get_system_tz() |     self.timezone = get_system_tz() | ||||||
|     self.round_temperature = True |     self.round_temperature = True | ||||||
|     self.round_windspeed = True |     self.round_windspeed = True | ||||||
|     self.use_beaufort = True |     self.use_beaufort = True | ||||||
|     self.show_wind_direction = False |     self.forecast_interval = 'daily' # daily # hourly | ||||||
|     self.use_wind_direction_icon = False |  | ||||||
|     self.forecast_interval = 'hourly' # daily # hourly |  | ||||||
|     self.locale = sys_locale()[0] |     self.locale = sys_locale()[0] | ||||||
|     self.weatherfont = ImageFont.truetype(fonts['weathericons-regular-webfont'], |     self.weatherfont = ImageFont.truetype(fonts['weathericons-regular-webfont'], | ||||||
|                                           size = self.fontsize) |                                           size = self.fontsize) | ||||||
| @@ -201,9 +210,6 @@ class weather: | |||||||
|     icon_medium = icon_small * 2 |     icon_medium = icon_small * 2 | ||||||
|     icon_large = icon_small * 3 |     icon_large = icon_small * 3 | ||||||
|  |  | ||||||
|     print('col_width=', col_width, 'row_height:', row_height) |  | ||||||
|     print('small, medium ,large:', icon_small, icon_medium, icon_large) |  | ||||||
|            |  | ||||||
|     # Calculate the x-axis position of each col |     # Calculate the x-axis position of each col | ||||||
|     col1 = spacing_top |     col1 = spacing_top | ||||||
|     col2 = col1 + col_width |     col2 = col1 + col_width | ||||||
| @@ -222,16 +228,8 @@ class weather: | |||||||
|     weather_icon_pos = (col1, row1) |     weather_icon_pos = (col1, row1) | ||||||
|     temperature_icon_pos = (col2, row1) |     temperature_icon_pos = (col2, row1) | ||||||
|     temperature_pos = (col2+icon_small, row1) |     temperature_pos = (col2+icon_small, row1) | ||||||
|      |  | ||||||
|     print('temp icon pos:', temperature_icon_pos) |  | ||||||
|     print('temp:', temperature_pos) |  | ||||||
|      |  | ||||||
|     humidity_icon_pos = (col2, row2) |     humidity_icon_pos = (col2, row2) | ||||||
|     humidity_pos = (col2+icon_small, row2) |     humidity_pos = (col2+icon_small, row2) | ||||||
|      |  | ||||||
|     print('hum icon pos:', humidity_icon_pos) |  | ||||||
|     print('hum pos:', humidity_pos) |  | ||||||
|  |  | ||||||
|     windspeed_icon_pos = (col2, row3) |     windspeed_icon_pos = (col2, row3) | ||||||
|     windspeed_pos = (col2+icon_small, row3) |     windspeed_pos = (col2+icon_small, row3) | ||||||
|  |  | ||||||
| @@ -344,7 +342,15 @@ class weather: | |||||||
|                                                          self.locale) |                                                          self.locale) | ||||||
|         return {'temp':temp_range, 'icon':status, 'stamp': weekday} |         return {'temp':temp_range, 'icon':status, 'stamp': weekday} | ||||||
|  |  | ||||||
|       fc_data = [calculate_forecast(days) for days in range (1,5)] |       forecasts = [calculate_forecast(days) for days in range (1,5)] | ||||||
|  |  | ||||||
|  |       fc_data = {} | ||||||
|  |       for forecast in forecasts: | ||||||
|  |         fc_data['fc'+str(forecasts.index(forecast)+1)] = { | ||||||
|  |           'temp':forecast['temp'], | ||||||
|  |           'icon':forecast['icon'], | ||||||
|  |           'stamp': forecast['stamp'] | ||||||
|  |           } | ||||||
|  |  | ||||||
|     for key,val in fc_data.items(): |     for key,val in fc_data.items(): | ||||||
|       logging.info((key,val)) |       logging.info((key,val)) | ||||||
| @@ -354,9 +360,6 @@ class weather: | |||||||
|     weather_icon = weather.get_weather_icon_name() |     weather_icon = weather.get_weather_icon_name() | ||||||
|     humidity = str(weather.get_humidity()) |     humidity = str(weather.get_humidity()) | ||||||
|     windspeed = weather.get_wind(unit='meters_sec')['speed'] |     windspeed = weather.get_wind(unit='meters_sec')['speed'] | ||||||
|     wind_angle = weather.get_wind()['deg'] |  | ||||||
|     wind_direction = ["N","NE","E","SE","S","SW","W","NW"][round( |  | ||||||
|       wind_angle/45) % 8] |  | ||||||
|     sunrise_raw = arrow.get(weather.get_sunrise_time()).to(self.timezone) |     sunrise_raw = arrow.get(weather.get_sunrise_time()).to(self.timezone) | ||||||
|     sunset_raw = arrow.get(weather.get_sunset_time()).to(self.timezone) |     sunset_raw = arrow.get(weather.get_sunset_time()).to(self.timezone) | ||||||
|  |  | ||||||
| @@ -384,10 +387,6 @@ class weather: | |||||||
|       elif self.units == 'imperial': |       elif self.units == 'imperial': | ||||||
|         wind = str(miles_per_hour) + 'mph' |         wind = str(miles_per_hour) + 'mph' | ||||||
|  |  | ||||||
|     if self.show_wind_direction == True: |  | ||||||
|       wind += '({0})'.format(wind_direction) |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     dec = decimal.Decimal |     dec = decimal.Decimal | ||||||
|     moonphase = get_moon_phase() |     moonphase = get_moon_phase() | ||||||
|  |  | ||||||
| @@ -402,7 +401,7 @@ class weather: | |||||||
|  |  | ||||||
|     if is_negative(temperature): |     if is_negative(temperature): | ||||||
|        write(im_black, temperature_pos, (col_width-icon_small, row_height), |        write(im_black, temperature_pos, (col_width-icon_small, row_height), | ||||||
|           temperature, font = self.font, fill_height = 0.9) |           temperature, font = self.font) | ||||||
|     else: |     else: | ||||||
|        write(im_black, temperature_pos, (col_width-icon_small, row_height), |        write(im_black, temperature_pos, (col_width-icon_small, row_height), | ||||||
|           temperature, font = self.font) |           temperature, font = self.font) | ||||||
| @@ -411,28 +410,24 @@ class weather: | |||||||
|           '\uf07a') |           '\uf07a') | ||||||
|  |  | ||||||
|     write(im_black, humidity_pos, (col_width-icon_small, row_height), |     write(im_black, humidity_pos, (col_width-icon_small, row_height), | ||||||
|           humidity+'%', font = self.font, fill_height = 0.9) |           humidity+'%', font = self.font) | ||||||
|  |  | ||||||
|     if self.use_wind_direction_icon == False: |     draw_icon(im_colour, windspeed_icon_pos, (icon_small, icon_small), | ||||||
|       draw_icon(im_colour, windspeed_icon_pos, (icon_small, icon_small), |  | ||||||
|                 '\uf050') |                 '\uf050') | ||||||
|     else: |  | ||||||
|       draw_icon(im_colour, windspeed_icon_pos, (icon_small, icon_small), |  | ||||||
|                 '\uf0b1', rotation = -wind_degrees)     |  | ||||||
|  |  | ||||||
|     write(im_black, windspeed_pos, (col_width-icon_small, row_height), |     write(im_black, windspeed_pos, (col_width-icon_small, row_height), | ||||||
|           wind, font=self.font, fill_height = 0.9) |           wind, font=self.font) | ||||||
|  |  | ||||||
|     # Fill weather details in col 3 (moonphase, sunrise, sunset) |     # Fill weather details in col 3 (moonphase, sunrise, sunset) | ||||||
|     draw_icon(im_colour, moonphase_pos, (col_width, row_height), moonphase) |     draw_icon(im_colour, moonphase_pos, (col_width, row_height), moonphase) | ||||||
|  |  | ||||||
|     draw_icon(im_colour, sunrise_icon_pos, (icon_small, icon_small), '\uf051') |     draw_icon(im_colour, sunrise_icon_pos, (icon_small, icon_small), '\uf051') | ||||||
|     write(im_black, sunrise_time_pos, (col_width-icon_small, icon_small), sunrise, |     write(im_black, sunrise_time_pos, (col_width-icon_small, icon_small), | ||||||
|           font = self.font, autofit = True) |           sunrise, font = self.font) | ||||||
|  |  | ||||||
|     draw_icon(im_colour, sunset_icon_pos, (icon_small, icon_small), '\uf052') |     draw_icon(im_colour, sunset_icon_pos, (icon_small, icon_small), '\uf052') | ||||||
|     write(im_black, sunset_time_pos, (col_width-icon_small, icon_small), sunset, |     write(im_black, sunset_time_pos, (col_width-icon_small, icon_small), sunset, | ||||||
|           font = self.font, autofit = True) |           font = self.font) | ||||||
|  |  | ||||||
|     # Add the forecast data to the correct places |     # Add the forecast data to the correct places | ||||||
|     for pos in range(1, len(fc_data)+1): |     for pos in range(1, len(fc_data)+1): | ||||||
| @@ -448,29 +443,20 @@ class weather: | |||||||
|         temp, font = self.font) |         temp, font = self.font) | ||||||
|  |  | ||||||
|     # Add borders around each sub-section |     # Add borders around each sub-section | ||||||
|     square_h = int((row_height*3)*0.9) |     draw_border(im_black, (col1, row1), (col_width*3, im_height), | ||||||
|     square_w = int((col_width*0.9)) |                 shrinkage=(0.02,0.1)) | ||||||
|     draw_square(im_colour, (col1, row1), (col_width*3, square_h)) |     draw_border(im_black, (col4, row1), (col_width, im_height)) | ||||||
|     draw_square(im_colour, (col4, row1), (square_w, square_h)) |     draw_border(im_black, (col5, row1), (col_width, im_height)) | ||||||
|     draw_square(im_colour, (col5, row1), (square_w, square_h)) |     draw_border(im_black, (col6, row1), (col_width, im_height)) | ||||||
|     draw_square(im_colour, (col6, row1), (square_w, square_h)) |     draw_border(im_black, (col7, row1), (col_width, im_height)) | ||||||
|     draw_square(im_colour, (col7, row1), (square_w, square_h)) |  | ||||||
|  | ############################################################################## | ||||||
|  | #        Error Handling | ||||||
|  | ############################################################################## | ||||||
|  |  | ||||||
| ##    except Exception as e: |  | ||||||
| ##      """If something went wrong, print a Error message on the Terminal""" |  | ||||||
| ##      print('Failed!') |  | ||||||
| ##      print('Error in weather module!') |  | ||||||
| ##      print('Reason: ',e) |  | ||||||
| ##      clear_image('top_section') |  | ||||||
| ##      write(top_section_width, top_section_height, str(e), |  | ||||||
| ##                 (0, 0), font = font) |  | ||||||
| ##      weather_image = crop_image(image, 'top_section') |  | ||||||
| ##      weather_image.save(image_path+'inkycal_weather.png') |  | ||||||
| ##      pass |  | ||||||
| ## |  | ||||||
|     # Save image of black and colour channel in image-folder |     # Save image of black and colour channel in image-folder | ||||||
|     im_black.save(images+self.name+'.png') |     im_black.save(images+self.name+'.png', "PNG") | ||||||
|     im_colour.save(images+self.name+'_colour.png') |     im_colour.save(images+self.name+'_colour.png', "PNG") | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|   print('running {0} in standalone mode'.format( |   print('running {0} in standalone mode'.format( | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user