Skip to main content

A layout companion library for Pimoroni's Inky HATs

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rpi-inky-layout-0.3.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rpi_inky_layout-0.3.0-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file rpi-inky-layout-0.3.0.tar.gz.

File metadata

  • Download URL: rpi-inky-layout-0.3.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for rpi-inky-layout-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6db812e4d25a30811c8e9e6e502772251b3d7fb5f071c93c7afb240280f79032
MD5 c39a91ae7154eab95f893e12e7bd29bb
BLAKE2b-256 ca036f50b8521b45a8bcc009aaa30127b03cd86b8df829724dcfa4296d58d91e

See more details on using hashes here.

File details

Details for the file rpi_inky_layout-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: rpi_inky_layout-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for rpi_inky_layout-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d8a15a8a031585216ffa4dd0b86d7d35aed7dbf2b91d7498030b0572185082c
MD5 aa5ad4702fb6eb357d1ef78d268e6426
BLAKE2b-256 a2c32898829849edc339e3d6c40535d12f54a09a3c85afb9877a26465686a2de

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page