diff --git a/README.md b/README.md index 26312e0..35ab6be 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,11 @@ To enable the weather display, set the environment variable `MODE` to `weather`. Next, use either `LATLONG` (e.g. 39.9199,32.8543) or `WEATHER_LOCATION` (e.g. Ankara, Turkey) environment variables to define the location for weather information. Entering only an empty `WEATHER_LOCATION` is also sufficient and in this case Inkyshot will lookup the latitude and longitude information from device's IP address. -Set `SCALE` environment variable to `F` to display the temperature values in Fahrenheit scale. The default is Celcius scale. +Set `SCALE` environment variable to `F` to display the temperature values in Fahrenheit scale. The default is Celsius scale. + +Set `TEMP_THRESHOLD` environment variable to a desired temperature. All readings above this value will be displayed in colour on colour eInk screens. The default value is 25 Celsius (77 Fahrenheit). + +You can use the `WEATHER_DARK_MODE` environment variable if you want to eanble dark mode on the inkyshot. Use the `WEATHER_FONT` variable to customize the font used in weather display mode. diff --git a/inkyshot/update-display.py b/inkyshot/update-display.py index 90ea9e8..df90685 100644 --- a/inkyshot/update-display.py +++ b/inkyshot/update-display.py @@ -78,32 +78,51 @@ def create_mask(source): mask_image.putpixel((x, y), 255) return mask_image +def swap_two_colours(img, dark_mode=False): + """Swap two colours. + """ + black = BLACK + if dark_mode: + target = BLACK - 1 + else: + target = BLACK + 1 + logging.info(f"Swapping colours {black} and {target}") + w, h = img.size + for x in range(w): + for y in range(h): + if img.getpixel((x, y)) == black: + img.putpixel((x, y), target) + elif img.getpixel((x, y)) == target: + img.putpixel((x, y), black) + return img + # Declare non pip fonts here ** Note: ttf files need to be in the /fonts dir of application repo Grand9KPixel = "/usr/app/fonts/Grand9KPixel.ttf" -def draw_weather(weather, img, scale): +def draw_weather(weather, img, scale, fill): """Draw the weather info on screen""" logging.info("Prepare the weather data for drawing") # Draw today's date on left side below today's name today = arrow.utcnow().format(fmt="DD MMMM", locale=LOCALE) - date_font = ImageFont.truetype(WEATHER_FONT, 18) - draw.text((3, 3), today, BLACK, font=date_font) + date_font = ImageFont.truetype(WEATHER_FONT, 18 + WEATHER_FONT_INCREASE) + draw.text((3 + X_OFFSET/3, 3 + Y_OFFSET), today, BLACK, font=date_font) # Draw current temperature to right of today - temp_font = ImageFont.truetype(WEATHER_FONT, 24) - draw.text((3, 30), f"{temp_to_str(weather['temperature'], scale)}°", BLACK, font=temp_font) + temp_font = ImageFont.truetype(WEATHER_FONT, 24 + WEATHER_FONT_INCREASE) + draw.text((3 + X_OFFSET/3, 30 + Y_OFFSET), f"{temp_to_str(weather['temperature'], scale)}°", fill, font=temp_font) # Draw today's high and low temps on left side below date - small_font = ImageFont.truetype(WEATHER_FONT, 14) + small_font = ImageFont.truetype(WEATHER_FONT, 14 + WEATHER_FONT_INCREASE) draw.text( - (3, 72), + (3 + X_OFFSET/3, 72 + Y_OFFSET), f"{temp_to_str(weather['min_temp'], scale)}° - {temp_to_str(weather['max_temp'], scale)}°", BLACK, font=small_font, ) # Draw today's max humidity on left side below temperatures - draw.text((3, 87), f"{weather['max_humidity']}%", BLACK, font=small_font) + draw.text((3 + X_OFFSET/3, 87 + Y_OFFSET), f"{weather['max_humidity']}%", BLACK, font=small_font) # Load weather icon icon_name = weather['symbol'].split('_')[0] time_of_day = '' + swap_colours = False # Couple of symbols have different icons for day and night. Check if this symbol is one of them. if len(weather['symbol'].split('_')) > 1: symbol_cycle = weather['symbol'].split('_')[1] @@ -111,19 +130,23 @@ def draw_weather(weather, img, scale): time_of_day = 'd' elif symbol_cycle == 'night': time_of_day = 'n' + swap_colours = True icon_filename = f"{icon_map[icon_name]:02}{time_of_day}.png" filepath = Path(__file__).parent / 'weather-icons' / icon_filename icon_image = Image.open(filepath) - icon_mask = create_mask(icon_image) + if swap_colours: + logging.info("Swapping night weather icon black and colour pixels") + icon_image = swap_two_colours(icon_image) # Draw the weather icon if WEATHER_INVERT and WAVESHARE: + icon_mask = create_mask(icon_image) logging.info("Inverting Weather Icon") icon = Image.new('1', (100, 100), 255) icon.paste(icon_image, (0,0), icon_mask) icon_inverted = ImageOps.invert(icon.convert('RGB')) - img.paste(icon_inverted, (120, 3)) + img.paste(icon_inverted, (119 + X_OFFSET, 3 + Y_OFFSET)) else: - img.paste(icon_image, (120, 3), icon_mask) + img.paste(icon_image, (119 + X_OFFSET, 3 + Y_OFFSET)) return img def get_current_display(): @@ -225,11 +248,15 @@ def set_current_display(val): logging.error(f"Failed to set current display to {val}. Error is: {err}") def temp_to_str(temp, scale): - """Prepare the temperature to draw based on the defined scale: Celcius or Fahrenheit""" + """Prepare the temperature to draw based on the defined scale: Celsius or Fahrenheit""" if scale == 'F': - temp = temp * 9/5 + 32 + temp = celsius_to_fahrenheit(temp) return f"{temp:.1f}" +def celsius_to_fahrenheit(temp): + """Convert Celsius to Fahrenheit""" + return temp * 9/5 + 32 + # Read the preset environment variables and overwrite the default ones if "DEBUG" in os.environ: logging.basicConfig(level=logging.DEBUG) @@ -268,6 +295,11 @@ def temp_to_str(temp, scale): # Temperature scale SCALE = 'F' if "SCALE" in os.environ and os.environ["SCALE"] == 'F' else 'C' +# Temperature threshold above which T is displayed in colour +TEMP_THRESHOLD = 25 if SCALE == 'C' else celsius_to_fahrenheit(25) +if "TEMP_THRESHOLD" in os.environ: + TEMP_THRESHOLD = os.environ['TEMP_THRESHOLD'] + # Locale formatting of date LOCALE = os.environ["LOCALE"] if "LOCALE" in os.environ else 'en' @@ -306,6 +338,22 @@ def temp_to_str(temp, scale): HEIGHT = display.HEIGHT BLACK = display.BLACK WHITE = display.WHITE + COLOUR = BLACK if display.colour == "black" else display.RED + if HEIGHT == 104: + # Display size: W 212 x H 104 + X_OFFSET = 0 + Y_OFFSET = 0 + WEATHER_FONT_INCREASE = 0 + else: + # Display size: W 250 x H 122 + # text margin is 3 + 1/3*X_OFFSET = 10 pixels from left border + # weather icon is 250 - (120 + 21 + 100) = 9 pixels from right border + X_OFFSET = 21 + # centering weather icons along y-axis: + # default padding is 3 => 3+8 + 100 (weather icon height) + 3+8 = 122 pixels + # thus, an addition offset of 8 pixels places the weather icon in the middle + Y_OFFSET = 8 + WEATHER_FONT_INCREASE = 2 img = Image.new("P", (WIDTH, HEIGHT)) draw = ImageDraw.Draw(img) @@ -340,7 +388,9 @@ def temp_to_str(temp, scale): os.environ['LATLONG'] = f"{LAT},{LONG}" # If weather is empty dictionary, fall back to drawing quote if len(weather) > 0: - img = draw_weather(weather, img, SCALE) + temperature = weather['temperature'] if SCALE == 'C' else celsius_to_fahrenheit(weather['temperature']) + fill = COLOUR if temperature >= TEMP_THRESHOLD else BLACK + img = draw_weather(weather, img, SCALE, fill) else: target_display = 'quote' elif target_display == 'quote': @@ -415,6 +465,12 @@ def temp_to_str(temp, scale): if "ROTATE" in os.environ: img = img.rotate(180) +# Enable dark mode +# #(it swaps the colour of the first two pixels in the palette) +if "WEATHER_DARK_MODE" in os.environ and target_display == 'weather': + logging.info("Switching to dark mode") + img = swap_two_colours(img, dark_mode=True) + if WAVESHARE: # epd does not have a set_image method. display.display(display.getbuffer(img)) diff --git a/inkyshot/weather-icons/01d.png b/inkyshot/weather-icons/01d.png index a72c08e..356bc05 100644 Binary files a/inkyshot/weather-icons/01d.png and b/inkyshot/weather-icons/01d.png differ diff --git a/inkyshot/weather-icons/01n.png b/inkyshot/weather-icons/01n.png index 0ee9e6c..5a7eee2 100644 Binary files a/inkyshot/weather-icons/01n.png and b/inkyshot/weather-icons/01n.png differ diff --git a/inkyshot/weather-icons/02d.png b/inkyshot/weather-icons/02d.png index b438dc7..f9781f9 100644 Binary files a/inkyshot/weather-icons/02d.png and b/inkyshot/weather-icons/02d.png differ diff --git a/inkyshot/weather-icons/02n.png b/inkyshot/weather-icons/02n.png index 15d496a..4745fab 100644 Binary files a/inkyshot/weather-icons/02n.png and b/inkyshot/weather-icons/02n.png differ diff --git a/inkyshot/weather-icons/03d.png b/inkyshot/weather-icons/03d.png index 2df26b9..8347488 100644 Binary files a/inkyshot/weather-icons/03d.png and b/inkyshot/weather-icons/03d.png differ diff --git a/inkyshot/weather-icons/03n.png b/inkyshot/weather-icons/03n.png index 6b302ee..d5d2dc9 100644 Binary files a/inkyshot/weather-icons/03n.png and b/inkyshot/weather-icons/03n.png differ diff --git a/inkyshot/weather-icons/04.png b/inkyshot/weather-icons/04.png index 6476e29..068e77c 100644 Binary files a/inkyshot/weather-icons/04.png and b/inkyshot/weather-icons/04.png differ diff --git a/inkyshot/weather-icons/05d.png b/inkyshot/weather-icons/05d.png index 4928375..697f3b8 100644 Binary files a/inkyshot/weather-icons/05d.png and b/inkyshot/weather-icons/05d.png differ diff --git a/inkyshot/weather-icons/05n.png b/inkyshot/weather-icons/05n.png index 07a3f29..bc091d6 100644 Binary files a/inkyshot/weather-icons/05n.png and b/inkyshot/weather-icons/05n.png differ diff --git a/inkyshot/weather-icons/06d.png b/inkyshot/weather-icons/06d.png index ca68088..3d12af7 100644 Binary files a/inkyshot/weather-icons/06d.png and b/inkyshot/weather-icons/06d.png differ diff --git a/inkyshot/weather-icons/06n.png b/inkyshot/weather-icons/06n.png index 9942de8..2a6e715 100644 Binary files a/inkyshot/weather-icons/06n.png and b/inkyshot/weather-icons/06n.png differ diff --git a/inkyshot/weather-icons/07d.png b/inkyshot/weather-icons/07d.png index 74ffc1e..821b3dc 100644 Binary files a/inkyshot/weather-icons/07d.png and b/inkyshot/weather-icons/07d.png differ diff --git a/inkyshot/weather-icons/07n.png b/inkyshot/weather-icons/07n.png index ed1d99b..8915b77 100644 Binary files a/inkyshot/weather-icons/07n.png and b/inkyshot/weather-icons/07n.png differ diff --git a/inkyshot/weather-icons/08d.png b/inkyshot/weather-icons/08d.png index db824ab..9e56f2f 100644 Binary files a/inkyshot/weather-icons/08d.png and b/inkyshot/weather-icons/08d.png differ diff --git a/inkyshot/weather-icons/08n.png b/inkyshot/weather-icons/08n.png index 256d0e8..c455810 100644 Binary files a/inkyshot/weather-icons/08n.png and b/inkyshot/weather-icons/08n.png differ diff --git a/inkyshot/weather-icons/09.png b/inkyshot/weather-icons/09.png index a7b9821..cf00b8e 100644 Binary files a/inkyshot/weather-icons/09.png and b/inkyshot/weather-icons/09.png differ diff --git a/inkyshot/weather-icons/10.png b/inkyshot/weather-icons/10.png index 213678c..509c3c7 100644 Binary files a/inkyshot/weather-icons/10.png and b/inkyshot/weather-icons/10.png differ diff --git a/inkyshot/weather-icons/11.png b/inkyshot/weather-icons/11.png index c3f75a8..e9037fc 100644 Binary files a/inkyshot/weather-icons/11.png and b/inkyshot/weather-icons/11.png differ diff --git a/inkyshot/weather-icons/12.png b/inkyshot/weather-icons/12.png index 17ab4c9..1e66203 100644 Binary files a/inkyshot/weather-icons/12.png and b/inkyshot/weather-icons/12.png differ diff --git a/inkyshot/weather-icons/13.png b/inkyshot/weather-icons/13.png index babc451..4ed222b 100644 Binary files a/inkyshot/weather-icons/13.png and b/inkyshot/weather-icons/13.png differ diff --git a/inkyshot/weather-icons/14.png b/inkyshot/weather-icons/14.png index c07cd63..0df9167 100644 Binary files a/inkyshot/weather-icons/14.png and b/inkyshot/weather-icons/14.png differ diff --git a/inkyshot/weather-icons/15.png b/inkyshot/weather-icons/15.png index aee903e..fa9f98d 100644 Binary files a/inkyshot/weather-icons/15.png and b/inkyshot/weather-icons/15.png differ diff --git a/inkyshot/weather-icons/20d.png b/inkyshot/weather-icons/20d.png index 2640dcb..da616ef 100644 Binary files a/inkyshot/weather-icons/20d.png and b/inkyshot/weather-icons/20d.png differ diff --git a/inkyshot/weather-icons/20n.png b/inkyshot/weather-icons/20n.png index ba8c2f2..a9a9fd4 100644 Binary files a/inkyshot/weather-icons/20n.png and b/inkyshot/weather-icons/20n.png differ diff --git a/inkyshot/weather-icons/21d.png b/inkyshot/weather-icons/21d.png index 2fe3680..405e18e 100644 Binary files a/inkyshot/weather-icons/21d.png and b/inkyshot/weather-icons/21d.png differ diff --git a/inkyshot/weather-icons/21n.png b/inkyshot/weather-icons/21n.png index 655f661..1b340d0 100644 Binary files a/inkyshot/weather-icons/21n.png and b/inkyshot/weather-icons/21n.png differ diff --git a/inkyshot/weather-icons/22.png b/inkyshot/weather-icons/22.png index 45d58c2..0e8f775 100644 Binary files a/inkyshot/weather-icons/22.png and b/inkyshot/weather-icons/22.png differ diff --git a/inkyshot/weather-icons/23.png b/inkyshot/weather-icons/23.png index c2613af..85bb8d1 100644 Binary files a/inkyshot/weather-icons/23.png and b/inkyshot/weather-icons/23.png differ diff --git a/inkyshot/weather-icons/24d.png b/inkyshot/weather-icons/24d.png index 138f53b..9182174 100644 Binary files a/inkyshot/weather-icons/24d.png and b/inkyshot/weather-icons/24d.png differ diff --git a/inkyshot/weather-icons/24n.png b/inkyshot/weather-icons/24n.png index cddaacd..196ba1f 100644 Binary files a/inkyshot/weather-icons/24n.png and b/inkyshot/weather-icons/24n.png differ diff --git a/inkyshot/weather-icons/25d.png b/inkyshot/weather-icons/25d.png index fc32400..6683f2b 100644 Binary files a/inkyshot/weather-icons/25d.png and b/inkyshot/weather-icons/25d.png differ diff --git a/inkyshot/weather-icons/25n.png b/inkyshot/weather-icons/25n.png index e217543..abe82b3 100644 Binary files a/inkyshot/weather-icons/25n.png and b/inkyshot/weather-icons/25n.png differ diff --git a/inkyshot/weather-icons/26d.png b/inkyshot/weather-icons/26d.png index 012d2fe..37e5932 100644 Binary files a/inkyshot/weather-icons/26d.png and b/inkyshot/weather-icons/26d.png differ diff --git a/inkyshot/weather-icons/26n.png b/inkyshot/weather-icons/26n.png index ca8a6fb..7a14838 100644 Binary files a/inkyshot/weather-icons/26n.png and b/inkyshot/weather-icons/26n.png differ diff --git a/inkyshot/weather-icons/27d.png b/inkyshot/weather-icons/27d.png index 8ac870a..23b2abc 100644 Binary files a/inkyshot/weather-icons/27d.png and b/inkyshot/weather-icons/27d.png differ diff --git a/inkyshot/weather-icons/27n.png b/inkyshot/weather-icons/27n.png index e8a2099..94885fe 100644 Binary files a/inkyshot/weather-icons/27n.png and b/inkyshot/weather-icons/27n.png differ diff --git a/inkyshot/weather-icons/28d.png b/inkyshot/weather-icons/28d.png index 3c2bdd6..a2b174a 100644 Binary files a/inkyshot/weather-icons/28d.png and b/inkyshot/weather-icons/28d.png differ diff --git a/inkyshot/weather-icons/28n.png b/inkyshot/weather-icons/28n.png index 082465b..8f7c3c8 100644 Binary files a/inkyshot/weather-icons/28n.png and b/inkyshot/weather-icons/28n.png differ diff --git a/inkyshot/weather-icons/29d.png b/inkyshot/weather-icons/29d.png index 7e52e21..b1ea804 100644 Binary files a/inkyshot/weather-icons/29d.png and b/inkyshot/weather-icons/29d.png differ diff --git a/inkyshot/weather-icons/29n.png b/inkyshot/weather-icons/29n.png index 552892b..b3add7b 100644 Binary files a/inkyshot/weather-icons/29n.png and b/inkyshot/weather-icons/29n.png differ diff --git a/inkyshot/weather-icons/30.png b/inkyshot/weather-icons/30.png index 05864f9..fd094e2 100644 Binary files a/inkyshot/weather-icons/30.png and b/inkyshot/weather-icons/30.png differ diff --git a/inkyshot/weather-icons/31.png b/inkyshot/weather-icons/31.png index 931a24a..071d503 100644 Binary files a/inkyshot/weather-icons/31.png and b/inkyshot/weather-icons/31.png differ diff --git a/inkyshot/weather-icons/32.png b/inkyshot/weather-icons/32.png index c9c39d5..e7cf3ff 100644 Binary files a/inkyshot/weather-icons/32.png and b/inkyshot/weather-icons/32.png differ diff --git a/inkyshot/weather-icons/33.png b/inkyshot/weather-icons/33.png index d683eb5..26f6984 100644 Binary files a/inkyshot/weather-icons/33.png and b/inkyshot/weather-icons/33.png differ diff --git a/inkyshot/weather-icons/34.png b/inkyshot/weather-icons/34.png index 512bb1f..ca3c4ce 100644 Binary files a/inkyshot/weather-icons/34.png and b/inkyshot/weather-icons/34.png differ diff --git a/inkyshot/weather-icons/40d.png b/inkyshot/weather-icons/40d.png index fab83ec..46597d2 100644 Binary files a/inkyshot/weather-icons/40d.png and b/inkyshot/weather-icons/40d.png differ diff --git a/inkyshot/weather-icons/40n.png b/inkyshot/weather-icons/40n.png index 74cb5db..e744702 100644 Binary files a/inkyshot/weather-icons/40n.png and b/inkyshot/weather-icons/40n.png differ diff --git a/inkyshot/weather-icons/41d.png b/inkyshot/weather-icons/41d.png index 71b3e82..a26bac6 100644 Binary files a/inkyshot/weather-icons/41d.png and b/inkyshot/weather-icons/41d.png differ diff --git a/inkyshot/weather-icons/41n.png b/inkyshot/weather-icons/41n.png index 8e80712..39cec17 100644 Binary files a/inkyshot/weather-icons/41n.png and b/inkyshot/weather-icons/41n.png differ diff --git a/inkyshot/weather-icons/42d.png b/inkyshot/weather-icons/42d.png index 52f1ec6..4f08c87 100644 Binary files a/inkyshot/weather-icons/42d.png and b/inkyshot/weather-icons/42d.png differ diff --git a/inkyshot/weather-icons/42n.png b/inkyshot/weather-icons/42n.png index 8d57143..2bff11e 100644 Binary files a/inkyshot/weather-icons/42n.png and b/inkyshot/weather-icons/42n.png differ diff --git a/inkyshot/weather-icons/43d.png b/inkyshot/weather-icons/43d.png index 1c7e06c..de4e31d 100644 Binary files a/inkyshot/weather-icons/43d.png and b/inkyshot/weather-icons/43d.png differ diff --git a/inkyshot/weather-icons/43n.png b/inkyshot/weather-icons/43n.png index 1084c08..2e03c78 100644 Binary files a/inkyshot/weather-icons/43n.png and b/inkyshot/weather-icons/43n.png differ diff --git a/inkyshot/weather-icons/44d.png b/inkyshot/weather-icons/44d.png index c26129b..de83116 100644 Binary files a/inkyshot/weather-icons/44d.png and b/inkyshot/weather-icons/44d.png differ diff --git a/inkyshot/weather-icons/44n.png b/inkyshot/weather-icons/44n.png index 0e4f059..b72006d 100644 Binary files a/inkyshot/weather-icons/44n.png and b/inkyshot/weather-icons/44n.png differ diff --git a/inkyshot/weather-icons/45d.png b/inkyshot/weather-icons/45d.png index ace212e..7eaaf1f 100644 Binary files a/inkyshot/weather-icons/45d.png and b/inkyshot/weather-icons/45d.png differ diff --git a/inkyshot/weather-icons/45n.png b/inkyshot/weather-icons/45n.png index d89f590..184407a 100644 Binary files a/inkyshot/weather-icons/45n.png and b/inkyshot/weather-icons/45n.png differ diff --git a/inkyshot/weather-icons/46.png b/inkyshot/weather-icons/46.png index 99cf699..91c1dfb 100644 Binary files a/inkyshot/weather-icons/46.png and b/inkyshot/weather-icons/46.png differ diff --git a/inkyshot/weather-icons/47.png b/inkyshot/weather-icons/47.png index c327114..86eaac8 100644 Binary files a/inkyshot/weather-icons/47.png and b/inkyshot/weather-icons/47.png differ diff --git a/inkyshot/weather-icons/48.png b/inkyshot/weather-icons/48.png index c49aaa7..2e85305 100644 Binary files a/inkyshot/weather-icons/48.png and b/inkyshot/weather-icons/48.png differ diff --git a/inkyshot/weather-icons/49.png b/inkyshot/weather-icons/49.png index df01af0..4148425 100644 Binary files a/inkyshot/weather-icons/49.png and b/inkyshot/weather-icons/49.png differ diff --git a/inkyshot/weather-icons/50.png b/inkyshot/weather-icons/50.png index 150ae0b..6905916 100644 Binary files a/inkyshot/weather-icons/50.png and b/inkyshot/weather-icons/50.png differ diff --git a/repo.yml b/repo.yml new file mode 100644 index 0000000..cec5434 --- /dev/null +++ b/repo.yml @@ -0,0 +1 @@ +type: generic \ No newline at end of file