使用 urllib 和 pil 调整 url 图像大小
Resize url image using urllib and pil
我想从 url 中检索图像,调整它的大小(以适当地适合 GUI),然后在 GUI 中显示该图像。我相信我已经掌握了基本前提……或者至少我知道如何检索和显示图像。我在下面提供了一些示例代码。
import pywapi, pprint, string, urllib, io
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry("1440x900+0+0")
fd = urllib.urlopen("http://images.intellicast.com/WxImages/Radar/den.gif")
imgFile = io.BytesIO(fd.read())
im = ImageTk.PhotoImage(Image.open(imgFile)) # <-- here
image = Label(root, image = im, bd =2)
image.grid(row = 8, column = 0, columnspan = 2, padx = 20, pady = 30)
# This is just some working code for resizing an image from a url
#fd = urllib.urlretrieve("http://images.intellicast.com/WxImages/Radar/den.gif", "den.gif")
#im1 = Image.open("den.gif")
#im_small = im1.resize((200, 200), Image.ANTIALIAS)
#im_small.save("den.gif")
root.mainloop()
所以这两个代码部分都有效,我只是不确定如何适当地组合它们。谢谢你的时间!
将 im_small
传递给 PhotoImage
而不是 Image.open
。
fd = urllib.urlretrieve("http://images.intellicast.com/WxImages/Radar/den.gif", "den.gif")
im1 = Image.open("den.gif")
im_small = im1.resize((200, 200), Image.ANTIALIAS)
im = ImageTk.PhotoImage(im_small)
image = Label(root, image = im, bd =2)
image.grid(row = 8, column = 0, columnspan = 2, padx = 20, pady = 30)
我想从 url 中检索图像,调整它的大小(以适当地适合 GUI),然后在 GUI 中显示该图像。我相信我已经掌握了基本前提……或者至少我知道如何检索和显示图像。我在下面提供了一些示例代码。
import pywapi, pprint, string, urllib, io
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.geometry("1440x900+0+0")
fd = urllib.urlopen("http://images.intellicast.com/WxImages/Radar/den.gif")
imgFile = io.BytesIO(fd.read())
im = ImageTk.PhotoImage(Image.open(imgFile)) # <-- here
image = Label(root, image = im, bd =2)
image.grid(row = 8, column = 0, columnspan = 2, padx = 20, pady = 30)
# This is just some working code for resizing an image from a url
#fd = urllib.urlretrieve("http://images.intellicast.com/WxImages/Radar/den.gif", "den.gif")
#im1 = Image.open("den.gif")
#im_small = im1.resize((200, 200), Image.ANTIALIAS)
#im_small.save("den.gif")
root.mainloop()
所以这两个代码部分都有效,我只是不确定如何适当地组合它们。谢谢你的时间!
将 im_small
传递给 PhotoImage
而不是 Image.open
。
fd = urllib.urlretrieve("http://images.intellicast.com/WxImages/Radar/den.gif", "den.gif")
im1 = Image.open("den.gif")
im_small = im1.resize((200, 200), Image.ANTIALIAS)
im = ImageTk.PhotoImage(im_small)
image = Label(root, image = im, bd =2)
image.grid(row = 8, column = 0, columnspan = 2, padx = 20, pady = 30)