阅读网络服务器上的清新jpg图像并在Tkinter中查看

huangapple go评论54阅读模式
英文:

Reading a refreshing jpg image on a webserver and viewing within Tkinter

问题

以下是您要翻译的代码部分:

import io
from PIL import Image, ImageTk

try:
    import Tkinter as tk
    from urllib2 import urlopen
except ImportError:
    import tkinter as tk
    from urllib.request import urlopen

stage = tk.Tk()
stage.title("jpg stream test")

url = "http://<url>:<port>/cgi-bin/viewer/video.jpg"

def test(url):
    img_url = urlopen(url)
    img_data = io.BytesIO(img_url.read())
    img_pil = Image.open(img_data).resize((1000, 800))
    img_tk = ImageTk.PhotoImage(img_pil)
    panel.configure(image=img_tk)
    stage.after(200, test(url))

panel = tk.Label(stage, image=ImageTk.PhotoImage(Image.open("placeholder.jpg")))
panel.pack(side="bottom", fill="both", expand=True)

stage.after(500, test(url))

stage.mainloop()

请注意,您的代码可能需要其他设置或调试以确保其正常工作。

英文:

Ok so for a project I have been trying to view a stream from a really old cctv camera (I have the owners permission) and the way it works is with a single set path ("/cgi-bin/viewer/video.jpg") which is a constantly changing jpg image. I am able to view it through VLC (with an incredibly unintuitive setup) or by constantly refreshing the aforementioned path. So what I am trying to do is make a program to read the image data and display it in tkinter, and then repeat it every couple seconds.

import io 
from PIL import Image,ImageTk
# makes it backwards compatible
try:
    import Tkinter as tk
    from urllib2 import urlopen
except ImportError:
    import tkinter as tk
    from urllib.request import urlopen

stage=tk.Tk()
stage.title(&quot;jpg stream test&quot;)

url=&quot;http://&lt;url&gt;:&lt;port&gt;/cgi-bin/viewer/video.jpg&quot;

def test(url):
    img_url=urlopen(url)
    img_data=io.BytesIO(img_url.read())
    img_pil=Image.open(img_data).resize((1000,800))
    img_tk=ImageTk.PhotoImage(img_pil)
    panel.configure(image=img_tk)
    stage.after(200, test(url)) # I have tried upping the delay, it did nothing

panel=tk.Label(stage,image=ImageTk.PhotoImage(Image.open(&quot;placeholder.jpg&quot;))) # tried it without placeholder
panel.pack(side=&quot;bottom&quot;,fill=&quot;both&quot;,expand=True)

stage.after(500,test(url))

stage.mainloop()

Now I am fairly new to programming as a whole let alone tkinter, when I run this code it opens up to a white empty window, and stays that way. Though I did note when taking all the code out of the test() function and removing all instances of stage.after() I could get a single image from the webstream with 0 issues. I am not sure what could possibly be causing it to not work. This is my 5th attempt.

答案1

得分: 2

以下是您要翻译的代码部分:

import io 
from PIL import Image, ImageTk

# 使其向后兼容
try:
    import Tkinter as tk
    from urllib2 import urlopen
except ImportError:
    import tkinter as tk
    from urllib.request import urlopen

stage = tk.Tk()
stage.title("jpg stream test")

url = "http://<url>:<port>/cgi-bin/viewer/video.jpg"

def test(url):
    img_url = urlopen(url)
    img_data = io.BytesIO(img_url.read())
    img_pil = Image.open(img_data).resize((1000, 800))
    img_tk = ImageTk.PhotoImage(img_pil)
    panel.configure(image=img_tk)
    panel.image = img_tk
    stage.after(200, test, url) 

panel = tk.Label(stage, image=ImageTk.PhotoImage(Image.open("placeholder.jpg")))
panel.pack(side="bottom", fill="both", expand=True)

stage.after(500, test, url)

stage.mainloop()

希望这对您有所帮助。

英文:

Revised code (thanks to @acw1668):

import io 
from PIL import Image,ImageTk
# makes it backwards compatible
try:
    import Tkinter as tk
    from urllib2 import urlopen
except ImportError:
    import tkinter as tk
    from urllib.request import urlopen

stage=tk.Tk()
stage.title(&quot;jpg stream test&quot;)

url=&quot;http://&lt;url&gt;:&lt;port&gt;/cgi-bin/viewer/video.jpg&quot;

def test(url):
    img_url=urlopen(url)
    img_data=io.BytesIO(img_url.read())
    img_pil=Image.open(img_data).resize((1000,800))
    img_tk=ImageTk.PhotoImage(img_pil)
    panel.configure(image=img_tk)
    panel.image= img_tk
    stage.after(200, test, url) 

panel=tk.Label(stage,image=ImageTk.PhotoImage(Image.open(&quot;placeholder.jpg&quot;))) 
panel.pack(side=&quot;bottom&quot;,fill=&quot;both&quot;,expand=True)

stage.after(500, test, url)

stage.mainloop()

huangapple
  • 本文由 发表于 2023年4月11日 09:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定