Created
August 26, 2023 11:21
-
-
Save arno12/3bcbe04569ccb2457647a3b2810ad4ee to your computer and use it in GitHub Desktop.
Updating the e-ink display with a latest Surrealist painting and its information every day
This file contains 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
import requests | |
from inky import InkyPHAT | |
from PIL import Image, ImageDraw, ImageFont | |
import os | |
import random | |
import qrcode | |
# Set up the Inky display | |
inky_display = InkyPHAT("red") | |
inky_display.set_border(inky_display.WHITE) | |
# Wikiart API endpoint to fetch random Surrealist painting data | |
api_endpoint = "https://www.wikiart.org/en/api/2/PaintingsByGenres?genre=surrealism&count=10" | |
# Constants for image dimensions and banner height | |
DISPLAY_WIDTH = 800 | |
DISPLAY_HEIGHT = 480 | |
BANNER_HEIGHT = int(DISPLAY_HEIGHT * 0.15) | |
def get_random_surrealist_painting(): | |
response = requests.get(api_endpoint) | |
paintings_data = response.json() | |
surreal_paintings = [painting for painting in paintings_data if "Surrealism" in painting["genre"]] | |
random_painting = random.choice(surreal_paintings) | |
return random_painting | |
def download_image(image_url): | |
response = requests.get(image_url) | |
image_path = "temp_image.jpg" | |
with open(image_path, "wb") as image_file: | |
image_file.write(response.content) | |
return image_path | |
def add_banner(image, text, banner_height): | |
draw = ImageDraw.Draw(image) | |
font = ImageFont.load_default() # You can choose another font if needed | |
banner = Image.new("L", (image.width, banner_height), color=0) | |
image.paste(banner, (0, image.height - banner_height)) | |
draw.text((10, image.height - banner_height + 5), text, fill=inky_display.BLACK, font=font) | |
def generate_qr_code(url): | |
qr = qrcode.QRCode( | |
version=1, | |
error_correction=qrcode.constants.ERROR_CORRECT_L, | |
box_size=10, | |
border=4, | |
) | |
qr.add_data(url) | |
qr.make(fit=True) | |
qr_img = qr.make_image(fill_color="black", back_color="white") | |
return qr_img | |
def main(): | |
random_painting = get_random_surrealist_painting() | |
image_url = random_painting["image"] | |
artist_name = random_painting["artistName"] | |
painting_title = random_painting["title"] | |
year = random_painting["year"] | |
wikiart_url = random_painting["paintingUrl"] | |
# Clear the display | |
inky_display.clear() | |
# Download and display the new image | |
image_path = download_image(image_url) | |
img = Image.open(image_path) | |
img = img.resize((inky_display.width, inky_display.height - BANNER_HEIGHT)) | |
# Add banner with details | |
banner_text = f"{painting_title} by {artist_name}, {year}" | |
add_banner(img, banner_text, BANNER_HEIGHT) | |
# Generate and embed QR code | |
qr_code = generate_qr_code(wikiart_url) | |
qr_code_position = (img.width - qr_code.width - 10, img.height - qr_code.height - 10) | |
img.paste(qr_code, qr_code_position) | |
# Display the image | |
inky_display.set_image(img) | |
inky_display.show() | |
# Clean up | |
os.remove(image_path) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment