英文:
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("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)) # I have tried upping the delay, it did nothing
panel=tk.Label(stage,image=ImageTk.PhotoImage(Image.open("placeholder.jpg"))) # tried it without placeholder
panel.pack(side="bottom",fill="both",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("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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论