Skip to content

Commit

Permalink
Replace usage of deprecated Pillow methods (#162)
Browse files Browse the repository at this point in the history
* doc: update pygame system requirements

* replace Image.ANTIALIAS with Image.LANCZOS

* update commandline help example

* replace usage of deprecated Pillow methods

* doc update

* fix lint error

* add py311

* doc update

* fix markup
  • Loading branch information
thijstriemstra authored Aug 3, 2023
1 parent a1ec777 commit 464812e
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
python-minor-version: [7, 8, 9, 10]
python-minor-version: [7, 8, 9, 10, 11]
name: Python 3.${{ matrix.python-minor-version }}
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MIT License
===========

Copyright (c) 2017-2022 Richard Hull and contributors
Copyright (c) 2017-2023 Richard Hull and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
66 changes: 35 additions & 31 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Assuming you are using a Raspberry Pi (running Debian Jessie or newer), follow t
instructions in the above repositories to wire up your display, then from a command-line::

$ sudo usermod -a -G i2c,spi,gpio pi
$ sudo apt install python3-dev python3-pip libfreetype6-dev libjpeg-dev build-essential
$ sudo apt install libsdl-dev libportmidi-dev libsdl-ttf2.0-dev libsdl-mixer1.2-dev libsdl-image1.2-dev
$ sudo apt install python3-dev python3-pip python3-numpy libfreetype6-dev libjpeg-dev build-essential
$ sudo apt install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev libportmidi-dev

Log out and in again and clone this repository::

Expand Down Expand Up @@ -93,29 +93,29 @@ flag to show the options::

$ python3 examples/demo.py --help
usage: demo.py [-h] [--config CONFIG] [--display DISPLAY] [--width WIDTH]
[--height HEIGHT] [--rotate ROTATION] [--interface INTERFACE]
[--i2c-port I2C_PORT] [--i2c-address I2C_ADDRESS]
[--spi-port SPI_PORT] [--spi-device SPI_DEVICE]
[--spi-bus-speed SPI_BUS_SPEED]
[--spi-transfer-size SPI_TRANSFER_SIZE]
[--spi-cs-high SPI_CS_HIGH] [--ftdi-device FTDI_DEVICE]
[--framebuffer-device FRAMEBUFFER_DEVICE] [--gpio GPIO]
[--gpio-mode GPIO_MODE]
[--gpio-data-command GPIO_DATA_COMMAND]
[--gpio-chip-select GPIO_CHIP_SELECT]
[--gpio-reset GPIO_RESET] [--gpio-backlight GPIO_BACKLIGHT]
[--gpio-reset-hold-time GPIO_RESET_HOLD_TIME]
[--gpio-reset-release-time GPIO_RESET_RELEASE_TIME]
[--block-orientation ORIENTATION] [--mode MODE]
[--framebuffer FRAMEBUFFER] [--num-segments NUM_SEGMENTS]
[--bgr] [--inverse] [--h-offset H_OFFSET]
[--v-offset V_OFFSET] [--backlight-active VALUE] [--debug]
[--transform TRANSFORM] [--scale SCALE] [--duration DURATION]
[--loop LOOP] [--max-frames MAX_FRAMES]
[--height HEIGHT] [--rotate ROTATION] [--interface INTERFACE]
[--i2c-port I2C_PORT] [--i2c-address I2C_ADDRESS]
[--spi-port SPI_PORT] [--spi-device SPI_DEVICE]
[--spi-bus-speed SPI_BUS_SPEED]
[--spi-transfer-size SPI_TRANSFER_SIZE]
[--spi-cs-high SPI_CS_HIGH] [--ftdi-device FTDI_DEVICE]
[--framebuffer-device FRAMEBUFFER_DEVICE] [--gpio GPIO]
[--gpio-mode GPIO_MODE]
[--gpio-data-command GPIO_DATA_COMMAND]
[--gpio-chip-select GPIO_CHIP_SELECT]
[--gpio-reset GPIO_RESET] [--gpio-backlight GPIO_BACKLIGHT]
[--gpio-reset-hold-time GPIO_RESET_HOLD_TIME]
[--gpio-reset-release-time GPIO_RESET_RELEASE_TIME]
[--block-orientation ORIENTATION] [--mode MODE]
[--framebuffer FRAMEBUFFER] [--num-segments NUM_SEGMENTS]
[--bgr] [--inverse] [--h-offset H_OFFSET]
[--v-offset V_OFFSET] [--backlight-active VALUE] [--debug]
[--transform TRANSFORM] [--scale SCALE] [--duration DURATION]
[--loop LOOP] [--max-frames MAX_FRAMES]

luma.examples arguments

optional arguments:
options:
-h, --help show this help message and exit

General:
Expand All @@ -126,11 +126,12 @@ flag to show the options::
Display type, supports real devices or emulators.
Allowed values are: ssd1306, ssd1309, ssd1322,
ssd1362, ssd1322_nhd, ssd1325, ssd1327, ssd1331,
ssd1351, sh1106, ws0010, winstar_weh, pcd8544, st7735,
ht1621, uc1701x, st7567, ili9341, ili9486, hd44780,
max7219, ws2812, neopixel, neosegment, apa102,
unicornhathd, capture, gifanim, pygame, asciiart,
asciiblock, linux_framebuffer (default: ssd1306)
ssd1351, sh1106, sh1107, ws0010, winstar_weh, pcd8544,
st7735, st7789, ht1621, uc1701x, st7567, ili9341,
ili9486, ili9488, hd44780, max7219, ws2812, neopixel,
neosegment, apa102, unicornhathd, capture, gifanim,
pygame, asciiart, asciiblock, linux_framebuffer
(default: ssd1306)
--width WIDTH Width of the device in pixels (default: 128)
--height HEIGHT Height of the device in pixels (default: 64)
--rotate ROTATION, -r ROTATION
Expand Down Expand Up @@ -252,13 +253,16 @@ and screen capture functionality:
* The `luma.emulator.device.pygame` device uses the `pygame` library to
render the displayed image to a pygame display surface.

*After installing* luma.emulator (see Documentation link below) you can invoke the demos with::
After `installing luma.emulator <https://luma-emulator.readthedocs.io/en/latest/install.html>`_
you can invoke the demos with::

$ python3 examples/clock.py --display capture
$ python3 examples/clock.py --display pygame

or::

$ python3 examples/clock.py --display pygame
$ python3 examples/clock.py --display gifanim

$ python3 examples/starfield.py --display capture

Documentation
-------------
Expand All @@ -274,7 +278,7 @@ License
-------
The MIT License (MIT)

Copyright (c) 2017-2022 Richard Hull & Contributors
Copyright (c) 2017-2023 Richard Hull & Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
20 changes: 16 additions & 4 deletions examples/colors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2020 Richard Hull and contributors
# Copyright (c) 2014-2023 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

Expand Down Expand Up @@ -32,7 +32,11 @@ def main():
for color in ["black", "white", "red", "orange", "yellow", "green", "blue", "indigo", "violet"]:
with canvas(device, dither=True) as draw:
draw.rectangle(device.bounding_box, fill=color)
size = draw.textsize(color)
# measure
left, top, right, bottom = draw.textbbox((0, 0), color)
size = right - left, bottom - top

# draw
left = (device.width - size[0]) // 2
top = (device.height - size[1]) // 2
right = left + size[0]
Expand All @@ -52,7 +56,11 @@ def main():
rgb = (r << 16) | (g << 8) | b
draw.rectangle((i * w, 0, (i + 1) * w, device.height), fill=rgb)

size = draw.textsize("rainbow")
# measure
left, top, right, bottom = draw.textbbox((0, 0), "rainbow")
size = right - left, bottom - top

# draw
left = (device.width - size[0]) // 2
top = (device.height - size[1]) // 2
right = left + size[0]
Expand All @@ -71,7 +79,11 @@ def main():
b = 0
draw.point((x, y), fill=(r, g, b))

size = draw.textsize("gradient")
# measure
left, top, right, bottom = draw.textbbox((0, 0), "gradient")
size = right - left, bottom - top

# draw
left = (device.width - size[0]) // 2
top = (device.height - size[1]) // 2
right = left + size[0]
Expand Down
38 changes: 23 additions & 15 deletions examples/demo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-18 Richard Hull and contributors
# Copyright (c) 2014-2023 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

Expand All @@ -18,34 +18,42 @@


def primitives(device, draw):
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
# Draw some shapes
# First define some constants to allow easy resizing of shapes
padding = 2
shape_width = 20
top = padding
bottom = device.height - padding - 1
# Move left to right keeping track of the current x position for drawing shapes.

# Move left to right keeping track of the current x position for drawing shapes
x = padding
# Draw an ellipse.

# Draw an ellipse
draw.ellipse((x, top, x + shape_width, bottom), outline="red", fill="black")
x += shape_width + padding
# Draw a rectangle.

# Draw a rectangle
draw.rectangle((x, top, x + shape_width, bottom), outline="blue", fill="black")
x += shape_width + padding
# Draw a triangle.

# Draw a triangle
draw.polygon([(x, bottom), (x + shape_width / 2, top), (x + shape_width, bottom)], outline="green", fill="black")
x += shape_width + padding
# Draw an X.

# Draw an X
draw.line((x, bottom, x + shape_width, top), fill="yellow")
draw.line((x, top, x + shape_width, bottom), fill="yellow")
x += shape_width + padding
# Write two lines of text.
size = draw.textsize('World!')
x = device.width - padding - size[0]
draw.rectangle((x, top + 4, x + size[0], top + size[1]), fill="black")
draw.rectangle((x, top + 16, x + size[0], top + 16 + size[1]), fill="black")
draw.text((device.width - padding - size[0], top + 4), 'Hello', fill="cyan")
draw.text((device.width - padding - size[0], top + 16), 'World!', fill="purple")

# Write two lines of text
left, t, right, bottom = draw.textbbox((0, 0), 'World!')
w, h = right - left, bottom - t
x = device.width - padding - w
draw.rectangle((x, top + 4, x + w, top + h), fill="black")
draw.rectangle((x, top + 16, x + w, top + 16 + h), fill="black")
draw.text((device.width - padding - w, top + 4), 'Hello', fill="cyan")
draw.text((device.width - padding - w, top + 16), 'World!', fill="purple")

# Draw a rectangle of the same size of screen
draw.rectangle(device.bounding_box, outline="white")

Expand Down
5 changes: 3 additions & 2 deletions examples/font_awesome.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2020 Richard Hull and contributors
# Copyright (c) 2014-2023 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

Expand Down Expand Up @@ -149,7 +149,8 @@ def main(num_iterations=sys.maxsize):
break

with canvas(device) as draw:
w, h = draw.textsize(text=code, font=font)
left, top, right, bottom = draw.textbbox((0, 0), code, font)
w, h = right - left, bottom - top
left = (device.width - w) / 2
top = (device.height - h) / 2
draw.text((left, top), text=code, font=font, fill="white")
Expand Down
9 changes: 6 additions & 3 deletions examples/game_of_life.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-18 Richard Hull and contributors
# Copyright (c) 2014-2023 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

Expand Down Expand Up @@ -40,6 +40,7 @@ def iterate(board):


def main():
text = "Game of Life"
scale = 3
cols = device.width // scale
rows = device.height // scale
Expand All @@ -61,11 +62,13 @@ def main():
draw.rectangle((left, top, right, bottom), fill="white", outline="black")

if i == 0:
w, h = draw.textsize("Game of Life")
left, top, right, bottom = draw.textbbox((0, 0), text)
w, h = right - left, bottom - top

left = (device.width - w) // 2
top = (device.height - h) // 2
draw.rectangle((left - 1, top, left + w + 1, top + h), fill="black", outline="white")
draw.text((left + 1, top), text="Game of Life", fill="white")
draw.text((left + 1, top), text=text, fill="white")

if i == 0:
time.sleep(3)
Expand Down
6 changes: 4 additions & 2 deletions examples/greyscale.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2020 Richard Hull and contributors
# Copyright (c) 2014-2023 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

Expand Down Expand Up @@ -37,7 +37,9 @@ def main():
rgb = (color << 16) | (color << 8) | color
draw.rectangle((i * w, 0, (i + 1) * w, device.height), fill=rgb)

size = draw.textsize("greyscale")
left, top, right, bottom = draw.textbbox((0, 0), 'greyscale')
size = right - left, bottom - top

left = (device.width - size[0]) // 2
top = (device.height - size[1]) // 2
right = left + size[0]
Expand Down
6 changes: 3 additions & 3 deletions examples/hotspot/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2022 Richard Hull and contributors
# Copyright (c) 2014-2023 Richard Hull and contributors
# See LICENSE.rst for details.

from pathlib import Path
Expand Down Expand Up @@ -34,10 +34,10 @@ def bytes2human(n, fmt="{0:0.2f}"):


def right_text(draw, y, width, margin, text):
x = width - margin - draw.textsize(text, font=tiny_font)[0]
x = width - margin - draw.textlength(text, font=tiny_font)
draw.text((x, y), text=text, font=tiny_font, fill="white")


def title_text(draw, y, width, text):
x = (width - draw.textsize(text)[0]) / 2
x = (width - draw.textlength(text)) / 2
draw.text((x, y), text=text, fill="yellow")
6 changes: 4 additions & 2 deletions examples/image_composition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2020 Richard Hull and contributors
# Copyright (c) 2014-2023 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

Expand All @@ -26,7 +26,9 @@
class TextImage():
def __init__(self, device, text, font):
with canvas(device) as draw:
w, h = draw.textsize(text, font)
left, top, right, bottom = draw.textbbox((0, 0), text, font)
w, h = right - left, bottom - top

self.image = Image.new(device.mode, (w, h))
draw = ImageDraw.Draw(self.image)
draw.text((0, 0), text, font=font, fill="white")
Expand Down
8 changes: 6 additions & 2 deletions examples/savepoint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2022 Richard Hull and contributors
# Copyright (c) 2014-2023 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

Expand All @@ -16,12 +16,16 @@

def render_box(draw, idx, color):
message = f"Nesting level: {idx}"
width, height = draw.textsize(message)
# measure
left, top, right, bottom = draw.textbbox((0, 0), message)
width, height = right - left, bottom - top

left = idx * 4
right = left + width + 2
top = idx * 4
bottom = top + height + 2

# draw
draw.rectangle((left, top, right, bottom), outline="white", fill="black")
draw.text((left + 2, top + 1), text=message, fill=color)

Expand Down
5 changes: 3 additions & 2 deletions examples/tweet_scroll.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-2022 Richard Hull and contributors
# Copyright (c) 2014-2023 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

Expand Down Expand Up @@ -67,7 +67,8 @@ def scroll_message(status, font=None, speed=1):

# First measure the text size
with canvas(device) as draw:
w, h = draw.textsize(full_text, font)
left, top, right, bottom = draw.textbbox((0, 0), full_text, font)
w, h = right - left, bottom - top

virtual = viewport(device, width=max(device.width, w + x + x), height=max(h, device.height))
with canvas(virtual) as draw:
Expand Down
Loading

0 comments on commit 464812e

Please sign in to comment.