Last active
May 3, 2021 09:50
-
-
Save sourceperl/fff898451b90b3332f8395bd658f5c57 to your computer and use it in GitHub Desktop.
Cast webcam jpeg image via redis DB also provide an example of tkinter client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from datetime import datetime | |
import time | |
import io | |
import sys | |
import urllib.request | |
# sudo pip3 install schedule | |
import schedule | |
# sudo apt install python3-pil python3-pil.imagetk | |
import PIL.Image | |
# sudo apt install python3-redis | |
import redis | |
# some const | |
REDIS_HOST='localhost' | |
REDIS_PORT=6379 | |
REDIS_KEY = 'webcam_img' | |
IMG_URL = 'https://www.infoclimat.fr/cartes/getProxyWebcam.php?idw=274&c=30&t=jpg&70610' | |
# some function | |
def webcam_polling_job(): | |
# log update | |
print('%s: update now' % datetime.now().strftime('%Y-%m-%dT%H:%M:%S')) | |
# do web request | |
try: | |
with urllib.request.urlopen(IMG_URL, timeout=10) as u: | |
raw_data = u.read() | |
# convert RAW img format (bytes) to Pillow image | |
pil_img = PIL.Image.open(io.BytesIO(raw_data)) | |
# resize to 1280x720 (720p) and force jpeg format | |
pil_img.thumbnail([1280, 720]) | |
io_to_redis = io.BytesIO() | |
pil_img.save(io_to_redis, format='JPEG') | |
# send jpeg data to redis | |
rc.set(REDIS_KEY, io_to_redis.getvalue()) | |
except Exception as err: | |
print(err, file=sys.stderr) | |
# init redis client | |
rc = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT) | |
# init scheduler | |
schedule.every().minutes.at(':30').do(webcam_polling_job) | |
webcam_polling_job() | |
# main loop | |
while True: | |
schedule.run_pending() | |
time.sleep(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from datetime import datetime | |
import io | |
import sys | |
import tkinter as tk | |
# sudo apt install python3-pil python3-pil.imagetk | |
import PIL.Image | |
import PIL.ImageTk | |
# sudo apt install python3-redis | |
import redis | |
# some const | |
REDIS_HOST='localhost' | |
REDIS_PORT=6379 | |
REDIS_KEY = 'webcam_img' | |
# build tk interface | |
class MainApp(tk.Tk): | |
def __init__(self, *args, **kwargs): | |
tk.Tk.__init__(self, *args, **kwargs) | |
# init redis client | |
self.rc = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT) | |
# img container | |
self.lbl_img = tk.Label() | |
self.lbl_img.pack() | |
# start auto-refresh | |
self.update_img() | |
def update_img(self): | |
# log update | |
print('%s: update now' % datetime.now().strftime('%Y-%m-%dT%H:%M:%S')) | |
# update tk image label | |
try: | |
# redis request | |
raw_data = self.rc.get(REDIS_KEY) | |
# RAW img data to Pillow (PIL) image | |
pil_img = PIL.Image.open(io.BytesIO(raw_data)) | |
# force size to 1280x720 | |
pil_img.thumbnail([1280, 720]) | |
# convert PIL image to Tk format | |
tk_img = PIL.ImageTk.PhotoImage(pil_img) | |
self.lbl_img.configure(image=tk_img) | |
# don't remove: keep a ref to avoid del by garbage collect | |
self.lbl_img.tk_img=tk_img | |
except Exception as err: | |
print(err, file=sys.stderr) | |
# redo after 2s | |
self.after(2000, func=self.update_img) | |
if __name__ == '__main__': | |
app = MainApp() | |
app.title('Webcam Fort-Mahon-Plage from redis DB') | |
app.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment