Add fetch_display_image script
This commit is contained in:
parent
6f847501cf
commit
862bb3c9e1
|
@ -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()
|
Loading…
Reference in New Issue