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:
- Use the
Layout
class to create a top-level Layout object. - Next, add new layers to a
Layout
- it automatically resizes the otherLayout
s at the same level. - Take each added sublayer
Layout
, and use itssize
to define a new PILImage
to draw on. - Draw on the image, Set the
Image
on the appropriateLayout
. - Invoke the top-level
Layout
'sdraw()
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 eitherh
(horizontal) orv
(vertical).border
is either a) the width of border (colour defaults to2
) 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
Built Distribution
Hashes for rpi_inky_layout-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58008576b5f0053bd364632ea58ad1e8153061dc959b71632d3ada8b9629eca3 |
|
MD5 | 854aa47b698043a4461d80b611ddbd08 |
|
BLAKE2b-256 | 3b420b7e675a9fbd14a34f6f0b6c74ab705bf4fc6dd7af1d651aa8665e31c569 |