0

I'm very new to pypng and with python itself, I've been working on a weather api program which is supposed to fetch an image from openweathermap.org. I've googled for hours and can't seem to find a solution for the error I'm getting. The code:

import tkinter
import io
from tkinter import font
import tkinter as tk
import time
import urllib.request
import json
import socket
from threading import Thread
import base64
import png


socket.getaddrinfo('localhost', 8080)


class Window(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)               
        self.master = master

def update_timeText():
    current = time.strftime("%H:%M")
    seconds = time.strftime(":%S")
    currentDate=time.strftime("%a %e %B, %Y")
    timeText1.configure(text=current, fg='white', background='black')
    timeText1.grid(row=0,column=0, sticky='NW', padx=15, pady=15)
    timeText2.configure(text=seconds, fg='white', background='black')
    timeText2.grid(row=0, column=1, pady=17, sticky='NW')
    Date.configure(text=currentDate, fg='white', background='black')
    Date.grid(row=0, column=0, columnspan=3, sticky='NW', padx=20, pady=124, rowspan=2)
    root.after(1000, update_timeText)

def update_Weather():
    temperatureData = weatherData["main"]["temp"]
    temperature = int(temperatureData) , "°C"
    weather = weatherData["weather"]
    List1 = weather[0]
    pictureCode = List1["icon"]
    picUrl = "http://openweathermap.org/img/w/"+pictureCode+".png"
    pictureData = png.Reader(file = urllib.request.urlopen(picUrl))
    picturePNG = pictureData.read()
    picture = png.Writer(picturePNG)
    print(picture)
    weatherIcon.configure(image=picture)
    weatherIcon.grid(row=3)
    weatherTemperature.configure(text=temperature, fg='white', background='black')
    weatherTemperature.grid(row=3, column=2)
    root.after(100000, update_Weather)

root = tk.Tk()
root.configure(background='black')
root.title('Smart Mirror')
timeText1 = tk.Label(root, text="", font=("Opinio", 90, "bold"))
timeText2 = tk.Label(root, text="", font=("Opinio", 45, "bold"))
Date=tk.Label(root, text="", font=("Roboto Condensed", 24))
weatherAPI=urllib.request.urlopen("https://api.openweathermap.org/data/2.5/weather?q=Mostar,070&APPID=d9c3aca43db397f6c24189c7c52948f9&units=metric")
weatherData=json.load(weatherAPI)
weatherTemperature=tk.Label(root, text="", font=("Roboto Condensed", 24))
weatherIcon=tk.Label(root, image="")
Thread(target=update_Weather).start()
Thread(target=update_timeText).start()
app = Window(root)
root.mainloop()

I get the icon code from JSON data and use it to create the url, weatherIcon is a label. The error I keep getting is:

png.ProtocolError: ProtocolError: width and height must be integers

Any help is very appreciated, many thanks.

K.Hodzic
  • 3
  • 3
  • `png.Writer(...)` expects the first two parameters are `width` and `height`, but `picturePNG` is a tuple of `(width, height, generator, info_dict)` (according to the document of `pypng`). From the document of `pypng`, it does not support conversion to tkinter image format. So try using `Pillow` module instead. – acw1668 Mar 13 '19 at 09:33

1 Answers1

0

Is there any reason you're calling your dictionary a list?

    pictureCode = List["icon"]

Anyhow are you using pypng for a specific reason? You could just go

    import requests
    from PIL import Image
    from io import BytesIO

    #Assuming this is actually a dictionary
    picId = dict["key"]
    image_url = "http://openweathermap.org/img/w/"+picId+".png"
    r = requests.get(image_url)

    i = Image.open(BytesIO(r.content))
    i.save("weather_icon.png")

What is weatherIcon? You should really post all of your code as it makes it easier for everyone else trying to help you.

MH21209
  • 17
  • 1
  • 6
  • Its a nested dictionary hence the list, weather icon is just a tk.Label, As for why im using pypng after a lot of googling i found that it best suits what i need. I edited in the full code in the question. – K.Hodzic Mar 12 '19 at 19:47