Skip to main content

RPi Inky Layout Manager

This is a layout manager I wrote to help me break up the display of a Pimoroni Inky-pHAT into seperate drawable areas.

You might find other uses for it - let me know if it's handy!

The outline process is:

  1. Use the Layout class to create a top-level Layout object.
  2. Next, add new layers to a Layout - it automatically resizes the other Layouts at the same level.
  3. Take each added sublayer Layout, and use its size to define a new PIL Image to draw on.
  4. Draw on the image, Set the Image on the appropriate Layout.
  5. Invoke the top-level Layout's draw() method to merge them all together, drawing borders as necessary.

And you end up with a nicely-spaced regions.

How it works

Create a new Layout:

layout = Layout( size, packingMode, border)

where:

  • size is a 2-part tuple of the form (width, height).
  • packingMode is the packing direction: currently either h (horizontal) or v (vertical).
  • border is either a) the width of border (colour defaults to 2) or b) a 2-tuple containing (width, colour).

Next, add multiple sub-layouts to layout

sublayout1 = layout.addLayer()
sublayout2 = layout.addLayer()

Then, for leach sublayout, use sublayout.size as the size of the image to draw on.

im1 = Image.new(mode, sublayout1.size, bgColour)
...

Draw on the image; Set it back on the sublayout:

sublayout1.setImage(im1)
...

And finally, draw the top level layout and write to file:

layout.draw()
layout.write(filename)

You can then do what you want with the file, e.g., load it onto an Inky display using setImage

Detailed usage example

Here's a detailed example.

Prerequisites

You will definitely need some sort of image manipulation library, such as pillow

from PIL import Image, ImageDraw

And, since this is built initially for use with Inky displays, you should probably import that, too:

from inky.auto import auto

Create new Layers

Create the top-level Layout:

toplevel = Level( (400,100), packingMode='h', border=(1,2))

Or, if you're using your inky:

board = auto()
toplevel = Level (board.resolution, 'h', (0,0) )

Add as many new layers as you want:

sublayout1 = layout.addLayer()
sublayout2 = layout.addLayer()
sublayout3 = layout.AddLayer()
sublayout31 = sublayout3.addLayer()
sublayout32 = sublayout3.addLayer()

Use the sub-layouts to create images:

mode = "P"
bgColour = 0
image31 = Image.new(mode, sublayout31.size, bgColour)
draw = ImageDraw.Draw(image31)
draw.text(tuple(s/2 for s in sublayout31.size), "Hello",1)
sublayout31.setImage(image31)

image32 = Image.new(mode, sublayout32.size, bgColour)
draw = ImageDraw.Draw(image32)
draw.text(tuple(s/2 for s in sublayout32.size), "World!",1)
sublayout32.setImage(image32)

layout.draw()
layout.write("hello-world.png")

If you're using the Inky, you can load the image up:

inky_image = Image.open("hello-world.png")
board.set_image(inky_image)

And you're done!

Advanced Features

Rotation

If you want to render the Layout in a rotated orientation, you can set the Rotation parameter. It takes the values:

  • UP - normal, no rotation;
  • LEFT - rotation by -90 or 270 degrees
  • RIGHT - rotation by +90 degrees
  • DOWN - rotated 180 degrees (up side down)

see under library/test/expected-images/test-rotated*.png for some examples.

Packing Bias

When adding a layer, you can specify packing bias:

layout = new Layout()
sublayout1 = layout.addLayer(packingBias=2)
sublayout2 = layout.addLayer()  # default packingBias=1

In this example, sublayout1 will take up 2/3rds of the space (2/(1+2)), while sublayout2 will be left with the remaining 1/3rd.

Metadata

Release files for rpi-inky-layout 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rpi-inky-layout 0.3.0
File Size Uploaded Provenance Actions
rpi-inky-layout-0.3.0.tar.gz 14.7 kB Without provenance Details for rpi-inky-layout-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rpi-inky-layout 0.3.0
File Interpreter ABI Platform Actions
rpi_inky_layout-0.3.0-py3-none-any.whl Python 3 none any Details for rpi_inky_layout-0.3.0-py3-none-any.whl

Total release size: 32.7 kB

Release files / rpi-inky-layout-0.3.0.tar.gz

Download URL rpi-inky-layout-0.3.0.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6db812e4d25a30811c8e9e6e502772251b3d7fb5f071c93c7afb240280f79032
BLAKE2b-256 checksum
How to use checksums
ca036f50b8521b45a8bcc009aaa30127b03cd86b8df829724dcfa4296d58d91e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1

Release files / rpi_inky_layout-0.3.0-py3-none-any.whl

Download URL rpi_inky_layout-0.3.0-py3-none-any.whl
Size 18.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d8a15a8a031585216ffa4dd0b86d7d35aed7dbf2b91d7498030b0572185082c
BLAKE2b-256 checksum
How to use checksums
a2c32898829849edc339e3d6c40535d12f54a09a3c85afb9877a26465686a2de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1

Security for rpi-inky-layout 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page