From 862bb3c9e1a3f24a61a44836f34d7160a0e0c5b4 Mon Sep 17 00:00:00 2001 From: Gal Date: Sat, 6 Apr 2024 00:54:42 +0200 Subject: [PATCH] Add fetch_display_image script --- rpi/fetch_display_image.py | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 rpi/fetch_display_image.py diff --git a/rpi/fetch_display_image.py b/rpi/fetch_display_image.py new file mode 100644 index 0000000..86436f9 --- /dev/null +++ b/rpi/fetch_display_image.py @@ -0,0 +1,71 @@ +import requests +from dotenv import load_dotenv +import os +import sys +import logging +from PIL import Image + +libdir = "./e-Paper/RaspberryPi_JetsonNano/python/lib" +if os.path.exists(libdir): + sys.path.append(libdir) +from waveshare_epd import epd7in5_V2 + +# Ref https://github.com/waveshareteam/e-Paper/blob/master/RaspberryPi_JetsonNano/python/examples/epd_7in5_V2_test.py + + +load_dotenv() + +def fetch_image(): + api_url = os.getenv("IMG_HOST_URL") + + try: + response = requests.get(api_url) + + if response.status_code == 200: + image_data = response.content + + with open("image.png", "wb") as file: + file.write(image_data) + + print("Image saved successfully as image.png") + return "image.png" + else: + print(f"Failed to fetch image: {response.status_code}") + return None + except Exception as e: + print(f"An error occurred: {e}") + return None + +def display_image(image_file): + try: + epd = epd7in5_V2.EPD() + logging.debug("Initializing e-Paper display") + epd.init() + + logging.debug(f"Reading image file: {image_file}") + Himage = Image.open(image_file) + logging.debug("Displaying image on screen") + epd.display(epd.getbuffer(Himage)) + + # Put the display to sleep + logging.debug("Putting display to sleep") + epd.sleep() + + except IOError as e: + logging.exception("IOError occurred: " + str(e)) + + except KeyboardInterrupt: + logging.debug("Keyboard Interrupt - Exiting") + epd7in5.epdconfig.module_exit() + sys.exit() + +def main(): + image_file = fetch_image() + if image_file: + display_image(image_file) + else: + logging.error("Failed to fetch image. Exiting.") + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') + main()