需要帮助将JPG链接转换为GIF格式 [英] Need help converting JPG link to GIF format

查看:129
本文介绍了需要帮助将JPG链接转换为GIF格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网站上获取图像以显示在Label小部件上,但是Tkinter仅接受GIF和BMP格式.我发现几乎所有图像都是JPG或PNG. 我现在拥有的代码如下:

I'm trying to get an image from a website to display on a Label widget but Tkinter only accepts GIF and BMP format. Pretty much all images I find are JPG or PNG. The code I have now is as follows:

from urllib import urlopen
from Tkinter import *
from PIL import Image
import Tkinter as tk

root = Tk()

url = "http://www.wired.com/wp-  
content/uploads/2015/03/10182025tonedfull- 
660x441.jpg"
u = urlopen(url)
raw_data = u.read()
u.close()
import base64
b64_data = base64.encodestring(raw_data)
image = tk.PhotoImage(data=b64_data)
label = tk.Label(image=image)
label.pack()

root.mainloop()

我收到一条错误消息,提示它无法识别图像.如果有人对我该如何解决有任何想法,请告诉我.谢谢.

I get an error message saying that it cannot recognize the image. If anyone has any idea how I can fix it please let me know. Thank you.

推荐答案

以下内容应允许您加载JPG文件:

The following should let you load JPG files:

from PIL import Image, ImageTk
from cStringIO import StringIO
from urllib import urlopen
from Tkinter import *
from PIL import Image
import Tkinter as tk

root = Tk()
url = "http://www.wired.com/wp-content/uploads/2015/03/10182025tonedfull-660x441.jpg"
u = urlopen(url)
raw_data = u.read()
u.close()

image_file = Image.open(StringIO(raw_data))
photo_image = ImageTk.PhotoImage(image_file)
label = tk.Label(image=photo_image)
label.pack()
root.mainloop()

您不需要对它进行base64编码,但是您确实需要使用ImageTk来加载图像.这将显示如下:

You do not need to base64 encode it, but you do need to use ImageTk to load the image. This would display as follows:

这篇关于需要帮助将JPG链接转换为GIF格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆