ScAlAn

Python get weather and display it on image

Task: Get current weather by city name and display it on my clock’s screen

Implementation:

# Get my city weather
    weather_json = requests.get('http://api.openweathermap.org/data/2.5/weather?
appid=<openweather_key_id>&q=<city_name>&units=metric').json()

    if weather_json["cod"] != "404":
        # Current temperature
        degree_sign = u"\N{DEGREE SIGN}"
        text = "T:" + str(int(weather_json["main"]["temp"])) + "(" + str(int(weather_json["main"]["feels_like"])) + ")" + degree_sign + "C"
        font = ImageFont.truetype('./freefont/FreeMonoBold.ttf', 38)
        image_width = font.getsize(text)
        img_txt = Image.new('L', image_width, 255)
        draw_txt = ImageDraw.Draw(img_txt)
        draw_txt.text((0, 0), text, font = font, fill = 0)
        t = img_txt.rotate(90, expand=1)
        #                y    x
        image.paste(t, (130,  int(260/2)-int(image_width[0]/2)))

Result you can see on this post image (third line).

Done.

Leave a Reply

Your email address will not be published. Required fields are marked *

  +  84  =  89