首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从带有pypng的url创建图像

无法从带有pypng的url创建图像
EN

Stack Overflow用户
提问于 2019-03-13 02:21:27
回答 1查看 234关注 0票数 0

我对pypng非常陌生,并且使用python本身,我一直在开发一个image程序,该程序应该从openweathermap.org获取图像。我用谷歌搜索了几个小时,似乎找不到解决我收到的错误的方法。代码:

代码语言:javascript
复制
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()

我从JSON数据中获取图标代码,并使用它来创建url,weatherIcon是一个标签。我一直得到的错误是:

代码语言:javascript
复制
png.ProtocolError: ProtocolError: width and height must be integers

任何帮助都非常感谢,非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2019-03-13 03:02:34

你把你的字典叫做列表有什么原因吗?

代码语言:javascript
复制
    pictureCode = List["icon"]

不管怎么说,你使用pypng有什么特别的原因吗?你可以直接走

代码语言:javascript
复制
    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")

什么是weatherIcon?你真的应该发布你所有的代码,因为这会让其他试图帮助你的人更容易。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55128315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档