简体   繁体   English

Python-从URL在Tkinter中打开图像-不使用PIL

[英]Python - Open image within Tkinter from URL - not using PIL

I need to display images within a tk window, but can only use these functions to do it; 我需要在tk窗口中显示图像,但是只能使用这些功能来完成。

from urllib.request import urlopen
from re import findall, MULTILINE, DOTALL
from webbrowser import open as webopen
from os import getcwd
from os.path import normpath
from tkinter import *
from sqlite3 import *
from datetime import datetime

I was thinking that there might be some way I can do this just through tkinter's standard functions, but have yet to find any possible way. 我当时以为可以通过tkinter的标准功能来实现此目的,但是还没有找到任何可能的方式。

This or I could try and find a way to convert the image to a GIF. 这或者我可以尝试找到一种将图像转换为GIF的方法。 Note that no images can be stored on the system. 请注意,无法在系统上存储任何图像。

The images I need to display are either JPGs or PNGs, such as this one. 我需要显示的图像是JPG或PNG,例如一幅。

Any help is appreciated. 任何帮助表示赞赏。

As is stated here : 正如说在这里

The PhotoImage class can read GIF and PGM/PPM images from files: PhotoImage类可以从文件读取GIF和PGM / PPM图像:

 photo = PhotoImage(file="image.gif") photo = PhotoImage(file="lenna.pgm") 

The PhotoImage can also read base64-encoded GIF files from strings. PhotoImage还可以从字符串读取base64编码的GIF文件。 You can use this to embed images in Python source code (use functions in the base64 module to convert binary data to base64-encoded strings): 您可以使用它在Python源代码中嵌入图像(使用base64模块中的函数将二进制数据转换为base64编码的字符串):

And as was pointed out in the comments below and here you can also use PNG images with PhotoImage . 正如下面的注释中所指出的,在这里您还可以将PNG图像与PhotoImage一起PhotoImage

Images in tkinter have to be passed in as PhotoImage objects to be drawn and in order to get a PhotoImage object, the image has to be one of the above formats. PhotoImage图像必须作为要绘制的PhotoImage对象传递,并且为了获得PhotoImage对象,该图像必须是上述格式之一。

So you will need to find a way to get the images in one of those formats using the modules you have available. 因此,您将需要找到一种使用现有模块以其中一种格式获取图像的方法。

The Tcl/Tk module Img allow you to load JPEG and PNG (among other formats) with PhotoImage. Tcl / Tk模块Img允许您使用PhotoImage加载JPEG和PNG(以及其他格式)。 This module is included in some packaged distributions of Tcl/Tk. 该模块包含在Tcl / Tk的某些打包发行版中。 So it is worth checking whether it is installed on the computer you are using. 因此,值得检查它是否已安装在您使用的计算机上。 To do so, try to load it with: 为此,请尝试将其加载:

widget.tk.eval('package require Img')

widget can be any Tkinter widget. widget可以是任何Tkinter小部件。

If it returns a version number, then it is installed and loaded so you can use it and open JPEG images simply doing 如果返回版本号,则说明已安装并加载它,因此您可以使用它并只需打开JPEG图像即可

PhotoImage(file='/my/image.jpg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM