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:
Ace 2020-05-13 18:36:54 +02:00
parent 33e6888096
commit b9b38b56bc
3 changed files with 70 additions and 117 deletions

View File

@ -139,6 +139,8 @@ def write(image, xy, box_size, text, font=None, **kwargs):
draw = ImageDraw.Draw(image)
space = Image.new('RGBA', (box_width, box_height))
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')
## ImageDraw.Draw(space).text((x, y), text, fill='white', font=font)
@ -179,22 +181,26 @@ def internet_available():
return False
def draw_square(image, xy, size, radius=5, thickness=2):
"""Draws a square with round corners at (x,y)
def draw_border(image, xy, size, radius=5, thickness=1, shrinkage=(0.1,0.1)):
"""Draws a border with round corners at (x,y)
xy = position e.g: (5,10)
size = size of square (width, height)
radius: corner radius
size = size of border (width, height), radius: corner radius
thickness = border thickness
shrinkage = shrink and center border by given percentage:(width_%, height_%)
"""
x, y, diameter = xy[0], xy[1], radius*2
colour='black'
width, height = size
lenght = width - diameter
# size from function paramter
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
p1, p2 = (x+radius, y), (x+radius+lenght, y)
p3, p4 = (x+width, y+radius), (x+width, y+radius+lenght)
p1, p2 = (x+radius, y), (x+radius+a, y)
p3, p4 = (x+width, y+radius), (x+width, y+radius+b)
p5, p6 = (p2[0], y+height), (p1[0], y+height)
p7, p8 = (x, p4[1]), (x,p3[1])
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)
##"""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?"""
##def clear_image(section, colour = background_colour):
## """Clear the image"""

View File

@ -12,17 +12,17 @@ try:
import feedparser
except ImportError:
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)
config = {'rss_urls': ['http://feeds.bbci.co.uk/news/world/rss.xml#']}
class rss:
"""RSS class
parses rss feeds from given urls and writes them on the image
parses rss feeds from given urls
"""
logger = logging.getLogger(__name__)
@ -142,8 +142,8 @@ class rss:
write(im_black, (0,0), (im_width, im_height), str(e), font = self.font)
# Save image of black and colour channel in image-folder
im_black.save(images+self.name+'.png')
im_colour.save(images+self.name+'_colour.png')
im_black.save(images+self.name+'.png', 'PNG')
im_colour.save(images+self.name+'_colour.png', 'PNG')
if __name__ == '__main__':
print('running {0} in standalone mode'.format(

View File

@ -6,15 +6,26 @@ Copyright by aceisace
"""
from inkycal.custom import *
import pyowm
import math, decimal
import arrow
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)
class weather:
"""weather class
parses weather details from openweathermap
"""
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
@ -34,18 +45,16 @@ class weather:
# Weather-specfic options
self.owm = pyowm.OWM(config['api_key'])
self.units = 'metric' # metric # imperial
self.hour_format = '12' # 12 #24
self.hour_format = '24' # 12 #24
self.timezone = get_system_tz()
self.round_temperature = True
self.round_windspeed = True
self.use_beaufort = True
self.show_wind_direction = False
self.use_wind_direction_icon = False
self.forecast_interval = 'hourly' # daily # hourly
self.forecast_interval = 'daily' # daily # hourly
self.locale = sys_locale()[0]
self.weatherfont = ImageFont.truetype(fonts['weathericons-regular-webfont'],
size = self.fontsize)
print('{0} loaded'.format(self.name))
def set(self, **kwargs):
@ -111,7 +120,7 @@ class weather:
"""Check if temp is below freezing point of water (0°C/30°F)
returns True if temp below freezing point, else False"""
answer = False
if temp_unit == 'celsius' and round(float(temp.split('°')[0])) <= 0:
answer = True
elif temp_unit == 'fahrenheit' and round(float(temp.split('°')[0])) <= 0:
@ -182,12 +191,12 @@ class weather:
# Calculate size rows and columns
col_width = im_width // 7
if (im_height // 3) > col_width//2:
row_height = (im_height // 4)
else:
row_height = (im_height // 3)
# Adjust the fontsize to make use of most free space
# self.font = auto_fontsize(self.font, row_height)
@ -201,9 +210,6 @@ class weather:
icon_medium = icon_small * 2
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
col1 = spacing_top
col2 = col1 + col_width
@ -222,16 +228,8 @@ class weather:
weather_icon_pos = (col1, row1)
temperature_icon_pos = (col2, row1)
temperature_pos = (col2+icon_small, row1)
print('temp icon pos:', temperature_icon_pos)
print('temp:', temperature_pos)
humidity_icon_pos = (col2, 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_pos = (col2+icon_small, row3)
@ -275,7 +273,7 @@ class weather:
temp_unit = 'celsius'
elif self.units == 'imperial':
temp_unit = 'fahrenheit'
# Get current time
now = arrow.utcnow()
@ -344,7 +342,15 @@ class weather:
self.locale)
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():
logging.info((key,val))
@ -354,9 +360,6 @@ class weather:
weather_icon = weather.get_weather_icon_name()
humidity = str(weather.get_humidity())
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)
sunset_raw = arrow.get(weather.get_sunset_time()).to(self.timezone)
@ -384,10 +387,6 @@ class weather:
elif self.units == 'imperial':
wind = str(miles_per_hour) + 'mph'
if self.show_wind_direction == True:
wind += '({0})'.format(wind_direction)
dec = decimal.Decimal
moonphase = get_moon_phase()
@ -402,7 +401,7 @@ class weather:
if is_negative(temperature):
write(im_black, temperature_pos, (col_width-icon_small, row_height),
temperature, font = self.font, fill_height = 0.9)
temperature, font = self.font)
else:
write(im_black, temperature_pos, (col_width-icon_small, row_height),
temperature, font = self.font)
@ -411,28 +410,24 @@ class weather:
'\uf07a')
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')
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),
wind, font=self.font, fill_height = 0.9)
wind, font=self.font)
# Fill weather details in col 3 (moonphase, sunrise, sunset)
draw_icon(im_colour, moonphase_pos, (col_width, row_height), moonphase)
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,
font = self.font, autofit = True)
write(im_black, sunrise_time_pos, (col_width-icon_small, icon_small),
sunrise, font = self.font)
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,
font = self.font, autofit = True)
font = self.font)
# Add the forecast data to the correct places
for pos in range(1, len(fc_data)+1):
@ -446,31 +441,22 @@ class weather:
icon)
write(im_black, eval('temp_fc'+str(pos)), (col_width, row_height),
temp, font = self.font)
# Add borders around each sub-section
square_h = int((row_height*3)*0.9)
square_w = int((col_width*0.9))
draw_square(im_colour, (col1, row1), (col_width*3, square_h))
draw_square(im_colour, (col4, row1), (square_w, square_h))
draw_square(im_colour, (col5, row1), (square_w, square_h))
draw_square(im_colour, (col6, row1), (square_w, square_h))
draw_square(im_colour, (col7, row1), (square_w, square_h))
## 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
##
# Add borders around each sub-section
draw_border(im_black, (col1, row1), (col_width*3, im_height),
shrinkage=(0.02,0.1))
draw_border(im_black, (col4, row1), (col_width, im_height))
draw_border(im_black, (col5, row1), (col_width, im_height))
draw_border(im_black, (col6, row1), (col_width, im_height))
draw_border(im_black, (col7, row1), (col_width, im_height))
##############################################################################
# Error Handling
##############################################################################
# Save image of black and colour channel in image-folder
im_black.save(images+self.name+'.png')
im_colour.save(images+self.name+'_colour.png')
im_black.save(images+self.name+'.png', "PNG")
im_colour.save(images+self.name+'_colour.png', "PNG")
if __name__ == '__main__':
print('running {0} in standalone mode'.format(