#!/usr/bin/env python3 """The weather icons of the dashboard and the app, made from Meteocons (MIT, Bas Milius, https://github.com/basmilius/meteocons): one PNG per OpenWeatherMap-style code ("01d", "10n" …), the names both sides look up (the dashboard's weather.WMO maps the WMO codes onto them). npm pack @meteocons/svg-static@0.1.0 && tar xzf meteocons-svg-static-0.1.0.tgz pip install cairosvg python3 tools/weather-icons.py package/fill → dashboard/plugins/weather/icons/.png and app/assets/weather/.png (the same files), square, the margin trimmed, plus Meteocons' LICENSE next to them. """ import io import os import shutil import sys import cairosvg from PIL import Image REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) OUT = [os.path.join(REPO, "dashboard", "plugins", "weather", "icons"), os.path.join(REPO, "app", "assets", "weather")] SIZE = 192 # code → (day, night): 01 clear, 02 mainly clear, 03 partly cloudy, 04 overcast, 09 drizzle / showers, # 10 rain, 11 thunderstorm, 13 snow, 50 fog ICONS = { "01": ("clear-day", "clear-night"), "02": ("partly-cloudy-day", "partly-cloudy-night"), "03": ("overcast-day", "overcast-night"), "04": ("overcast", "overcast"), "09": ("drizzle", "drizzle"), "10": ("rain", "rain"), "11": ("thunderstorms-rain", "thunderstorms-rain"), "13": ("snow", "snow"), "50": ("fog", "fog"), } def render(svg): im = Image.open(io.BytesIO(cairosvg.svg2png(url=svg, output_width=512, output_height=512))).convert("RGBA") im = im.crop(im.getchannel("A").point(lambda a: 255 if a > 8 else 0).getbbox()) # the margin off side = max(im.size) sq = Image.new("RGBA", (side, side), (0, 0, 0, 0)) sq.paste(im, ((side - im.width) // 2, (side - im.height) // 2)) return sq.resize((SIZE, SIZE), Image.LANCZOS) def main(): if len(sys.argv) != 2: sys.exit(__doc__) src = sys.argv[1] for d in OUT: os.makedirs(d, exist_ok=True) for f in os.listdir(d): # the previous set (e.g. other names) goes if f.endswith(".png"): os.remove(os.path.join(d, f)) for code, pair in ICONS.items(): for suffix, name in zip("dn", pair): im = render(os.path.join(src, name + ".svg")) for d in OUT: im.save(os.path.join(d, f"{code}{suffix}.png"), optimize=True) lic = os.path.join(os.path.dirname(os.path.abspath(src)), "LICENSE") for d in OUT: shutil.copyfile(lic, os.path.join(d, "LICENSE-meteocons.txt")) print(f"{len(ICONS) * 2} icons → " + ", ".join(os.path.relpath(d, REPO) for d in OUT)) if __name__ == "__main__": main()