Skip to content

Examples: Add hello_world.py and comic.py #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Python library for [Inky pHAT](https://shop.pimoroni.com/products/inky-phat), [I

### Inky Impression

[Inky Impression](https://shop.pimoroni.com/?q=inky+impression) is our line of glorious 7 colour eInk displays, available in [4"](https://shop.pimoroni.com/products/inky-impression-4) (640 x 400 pixel) [5.7"](https://shop.pimoroni.com/products/inky-impression-5-7) (600 x 448 pixel) and [7.3"](https://shop.pimoroni.com/products/inky-impression-7-3) (800 x 480 pixel) flavours. They're packed with strong colours and perfect for displaying striking graphics or lots of data.
[Inky Impression](https://shop.pimoroni.com/search?q=inky%20impression) is our line of glorious colour eInk displays, available in various sizes from the petite 4.0" up to the mighty 13.3". They're packed with strong colours and perfect for displaying striking graphics or lots of data.

## Installation

Expand Down
25 changes: 25 additions & 0 deletions examples/7color/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This basic example shows how to draw shapes and text on Inky Impression using PIL.

from font_fredoka_one import FredokaOne
from PIL import Image, ImageDraw, ImageFont

from inky.auto import auto

inky_display = auto(ask_user=True, verbose=True)

# Create new PIL image with a white background
image = Image.new("P", (inky_display.width, inky_display.height), inky_display.WHITE)
draw = ImageDraw.Draw(image)

font = ImageFont.truetype(FredokaOne, 72)

# draw some shapes
draw.rectangle((50, 50, 200, 200), fill=inky_display.YELLOW) # Rectangle
draw.ellipse((150, 150, 300, 300), fill=inky_display.RED) # Circle (ellipse)
draw.line((0, 0, 400, 400), fill=inky_display.BLUE, width=10) # Diagonal line

# draw some text
draw.text((0, 0), "Hello, World!", inky_display.BLACK, font)

inky_display.set_image(image)
inky_display.show()
33 changes: 33 additions & 0 deletions examples/spectra6/comics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# comic.py

`comic.py` fetches and displays a random comic book cover from the Comic Vine API.

- [comic.py](#comicpy)
- [About Comic Vine](#about-comic-vine)
- [Pre-requisites](#pre-requisites)
- [Usage](#usage)
- [Notes](#notes)

You can find a more detailed run down of how to use this script in our Learn Guide:

- [Learn: Displaying Comics on Inky Impression](https://learn.pimoroni.com/article/comics-on-inky-impression)

## About Comic Vine

Comic Vine is an awesome online database of comic book information. It has a free API that can be used to search and retrieve detailed information about comics, including cover images, issue details, and more. Check it out at https://comicvine.gamespot.com/ !

## Pre-requisites

You'll need to have the Inky library installed and your virtual environment activated: `source ~/.virtualenvs/pimoroni/bin/activate`

## Usage

1. Get a Comic Vine API key: [https://comicvine.gamespot.com/api/](https://comicvine.gamespot.com/api/)
2. Edit the script to add your API key (`API_KEY` variable).
3. Run the script: `python comic.py`
4. The script will fetch a random comic cover and display it on your Inky Impression.

## Notes

- You can change which volumes/series are searched by adding or removing strings from the `SEARCH_QUERIES` variable - the default is 'Weird Science', which looks like a great read.
- Set `RANDOM_VOLUME = True` to select a random volume from the top 5 search results instead of choosing the first one. We found this varied things up when there were multiple volumes with the same title.
97 changes: 97 additions & 0 deletions examples/spectra6/comics/comic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""
Python script to fetch a randomised comic cover from the Comic Vine API and show it on an Inky Impression display.
You will need to sign up for an API key at https://comicvine.gamespot.com/api/ to use this script.
Change the search query to the comic series you want to display!
"""

import random
from io import BytesIO

import requests
from PIL import Image

from inky.auto import auto

# Comic Vine API details
API_KEY = "API_KEY_GOES_HERE"
BASE_URL = "https://comicvine.gamespot.com/api/"
HEADERS = {"User-Agent": "Python Comic Vine Client"}

# List of comic series to display, separated by commas. You can add more series to this list, or change the existing ones.
SEARCH_QUERIES = ["Weird Science"]
# Set to True to pick a random volume from the query results (this is helpful if the series is split into multiple volumes):
RANDOM_VOLUME = False

# Inky Impression display setup
inky_display = auto()


def find_volume_id(api_key, query):
# Our first API call finds a list of volumes that match the search query, and then picks one
params = {"api_key": api_key, "format": "json", "query": query, "resources": "volume", "limit": 5}
response = requests.get(f"{BASE_URL}search/", headers=HEADERS, params=params)
response.raise_for_status()
data = response.json()
response.close()
results = data.get("results", [])
if results:
for idx, volume in enumerate(results, 1):
print(f"{idx}: {volume['name']} (ID: {volume['id']}, Start Year: {volume.get('start_year', 'N/A')})")

if RANDOM_VOLUME is True:
# Pick a random volume from the search results
chosen = random.choice(results)
print(f"Randomly selected: {chosen['name']} (ID: {chosen['id']})")
else:
# Pick the first volume from the search results
chosen = results[0]
print("Picked first result!")
return chosen["id"]
else:
raise ValueError("No volumes found for the given query.")


def fetch_random_comic_image(api_key, series_id):
# Once we know the volume ID we can do a second API call to fetch a list of issues and pick a random cover image
params = {"api_key": api_key, "format": "json", "filter": f"volume:{series_id}", "limit": 100}
response = requests.get(f"{BASE_URL}issues/", headers=HEADERS, params=params)
response.raise_for_status()
data = response.json()
response.close()
results = data.get("results", [])
if results:
issue = random.choice(results)
# print a link to the issue page on Comic Vine
print(f"Random issue selected: ID: {issue['id']}")
print(f"Find out more: {issue['site_detail_url']}")
image_link = issue["image"]["original_url"]
return image_link
else:
raise ValueError("No comic issues found for the specified series.")


def display_image_on_inky(image_url):
# Display image on Inky Impression
response = requests.get(image_url)
response.raise_for_status()
image = Image.open(BytesIO(response.content))
response.close()

# Rotate the image if it is taller than it is wide
if image.height > image.width:
image = image.rotate(90, expand=True)

image = image.resize(inky_display.resolution)
inky_display.set_image(image)
print("Updating Inky Impression!")
inky_display.show()


try:
# Pick a random search term from the list
search_query = random.choice(SEARCH_QUERIES)
volume_id = find_volume_id(API_KEY, search_query)
comic_image_url = fetch_random_comic_image(API_KEY, volume_id)
display_image_on_inky(comic_image_url)
except Exception as e:
print(f"Error: {e}")
25 changes: 25 additions & 0 deletions examples/spectra6/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This basic example shows how to draw shapes and text on Inky Impression using PIL.

from font_fredoka_one import FredokaOne
from PIL import Image, ImageDraw, ImageFont

from inky.auto import auto

inky_display = auto(ask_user=True, verbose=True)

# Create new PIL image with a white background
image = Image.new("P", (inky_display.width, inky_display.height), inky_display.WHITE)
draw = ImageDraw.Draw(image)

font = ImageFont.truetype(FredokaOne, 72)

# draw some shapes
draw.rectangle((50, 50, 200, 200), fill=inky_display.YELLOW) # Rectangle
draw.ellipse((150, 150, 300, 300), fill=inky_display.RED) # Circle (ellipse)
draw.line((0, 0, 400, 400), fill=inky_display.BLUE, width=10) # Diagonal line

# draw some text
draw.text((0, 0), "Hello, World!", inky_display.BLACK, font)

inky_display.set_image(image)
inky_display.show()