idotmatrix-api-client 1.1.0__tar.gz → 1.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/PKG-INFO +3 -3
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/README.md +2 -2
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/alarm.py +1 -1
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/gif.py +6 -5
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/image.py +12 -12
- idotmatrix_api_client-1.2.0/idotmatrix/screensize.py +23 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/app.py +7 -6
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/canvas.py +23 -15
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/convert.py +5 -5
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/draw.py +13 -13
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/preview.py +33 -21
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/previews.py +1 -1
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/device_info.py +1 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/image_utils.py +37 -19
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/pyproject.toml +1 -1
- idotmatrix_api_client-1.1.0/idotmatrix/screensize.py +0 -8
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/LICENSE +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/__init__.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/apps/__init__.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/apps/digital_picture_frame.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/client.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/connection_manager.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/const.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-ErbosDraco1StOpenNbpRegular-l5wX.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-MinecraftStandard.otf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-MinecraftStandardBoldOblique.otf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-PixelOperator8-Bold.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-PixelOperator8.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-PixelifySans-Regular.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-PressStart2P-Regular.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogica.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogicabold.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogicapixel.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogicapixelbold.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16_32-HomeVideo-BLG6G.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16_32-HomeVideoBold-R90Dv.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16_32-MayfairNbpBold-gAA4.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/32-Jersey10-Regular.ttf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/CC-BY-SA-4.0-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/ErbosDraco-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/HomeVideo-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/Jersey10-Regular-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/MayfairNbpBold-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/MinecraftStandard-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/PixelOperator-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/PixelifySans-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/PressStart2P-Regular-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/Rain-DRM3-License.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/Rain-DRM3.otf +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/dogica_license.txt +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/__init__.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/chronograph.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/clock.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/common.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/countdown.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/eco.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/effect.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/fullscreen_color.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/graffiti.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/materials.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/music_sync.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/scoreboard.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/system.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/text.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/protocol.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/transport.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/__init__.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/__main__.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/colorwheel.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/controls.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/devicebar.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/devices.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/dialogs.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/features.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/files.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/forms.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/pages.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/scanner.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/scoreboard.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/settings.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/widgets.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/__init__.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/color_utils.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/command_ack.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/device_material.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/file_watch.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/overlay.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/quality.py +0 -0
- {idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/upload_ack.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: idotmatrix-api-client
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: A library for controlling iDotMatrix displays.
|
|
5
5
|
License-Expression: GPL-3.0-or-later
|
|
6
6
|
License-File: LICENSE
|
|
@@ -28,7 +28,7 @@ Description-Content-Type: text/markdown
|
|
|
28
28
|
<h3 align="center">idotmatrix-api-client</h3>
|
|
29
29
|
|
|
30
30
|
<p align="center">
|
|
31
|
-
Control 16x16, 32x32 and
|
|
31
|
+
Control 16x16, 32x32, 64x64 and 24x48 sized iDotMatrix Pixel Displays
|
|
32
32
|
</p>
|
|
33
33
|
|
|
34
34
|
<p align="center">
|
|
@@ -57,7 +57,7 @@ this library.
|
|
|
57
57
|
* Alarms, which make the device beep (untested, see [the protocol notes](docs/DEVICE-PROTOCOL.md#alarm-and-buzzer))
|
|
58
58
|
* Digital picture frame for a folder of pictures
|
|
59
59
|
* Terminal UI to try all of it
|
|
60
|
-
* Works with the 16x16, 32x32 and 64x64 displays, with `bleak` or with the Bluetooth stack of Home Assistant
|
|
60
|
+
* Works with the 16x16, 32x32 and 64x64 displays and the 24x48 (48 pixels wide, 24 high), with `bleak` or with the Bluetooth stack of Home Assistant
|
|
61
61
|
|
|
62
62
|
Open ends (the cloud API, password protection, firmware update) are collected in the
|
|
63
63
|
[device protocol notes](docs/DEVICE-PROTOCOL.md#not-understood-yet).
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
<h3 align="center">idotmatrix-api-client</h3>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
|
-
Control 16x16, 32x32 and
|
|
10
|
+
Control 16x16, 32x32, 64x64 and 24x48 sized iDotMatrix Pixel Displays
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
13
|
<p align="center">
|
|
@@ -36,7 +36,7 @@ this library.
|
|
|
36
36
|
* Alarms, which make the device beep (untested, see [the protocol notes](docs/DEVICE-PROTOCOL.md#alarm-and-buzzer))
|
|
37
37
|
* Digital picture frame for a folder of pictures
|
|
38
38
|
* Terminal UI to try all of it
|
|
39
|
-
* Works with the 16x16, 32x32 and 64x64 displays, with `bleak` or with the Bluetooth stack of Home Assistant
|
|
39
|
+
* Works with the 16x16, 32x32 and 64x64 displays and the 24x48 (48 pixels wide, 24 high), with `bleak` or with the Bluetooth stack of Home Assistant
|
|
40
40
|
|
|
41
41
|
Open ends (the cloud API, password protection, firmware update) are collected in the
|
|
42
42
|
[device protocol notes](docs/DEVICE-PROTOCOL.md#not-understood-yet).
|
|
@@ -113,7 +113,7 @@ class AlarmModule(IDotMatrixModule):
|
|
|
113
113
|
gif_data = await asyncio.to_thread(
|
|
114
114
|
self._gif._load_gif_and_adapt_to_canvas,
|
|
115
115
|
file_path=file_path,
|
|
116
|
-
canvas_size=self._gif.screen_size.value
|
|
116
|
+
canvas_size=self._gif.screen_size.value,
|
|
117
117
|
resize_mode=resize_mode,
|
|
118
118
|
background_color=background_color,
|
|
119
119
|
)
|
|
@@ -105,14 +105,13 @@ class GifModule(IDotMatrixModule):
|
|
|
105
105
|
if slot is not None:
|
|
106
106
|
validate_slot(slot)
|
|
107
107
|
validate_display_time(display_time)
|
|
108
|
-
screen_width = self.screen_size.value[0] # assuming square canvas, so width == height
|
|
109
108
|
background_color = color_utils.parse_color_rgb(background_color)
|
|
110
109
|
|
|
111
110
|
# reading and converting the file takes a while, the event loop must not be blocked
|
|
112
111
|
gif_data = await asyncio.to_thread(
|
|
113
112
|
self._load_gif_and_adapt_to_canvas,
|
|
114
113
|
file_path=file_path,
|
|
115
|
-
canvas_size=
|
|
114
|
+
canvas_size=self.screen_size.value,
|
|
116
115
|
resize_mode=resize_mode,
|
|
117
116
|
palletize=palletize,
|
|
118
117
|
background_color=background_color,
|
|
@@ -150,7 +149,7 @@ class GifModule(IDotMatrixModule):
|
|
|
150
149
|
def _load_gif_and_adapt_to_canvas(
|
|
151
150
|
self,
|
|
152
151
|
file_path: PathLike | str,
|
|
153
|
-
canvas_size: int,
|
|
152
|
+
canvas_size: int | Tuple[int, int],
|
|
154
153
|
resize_mode: ResizeMode,
|
|
155
154
|
palletize: bool = True,
|
|
156
155
|
background_color: Tuple[int, int, int] = (0, 0, 0),
|
|
@@ -166,7 +165,8 @@ class GifModule(IDotMatrixModule):
|
|
|
166
165
|
|
|
167
166
|
Args:
|
|
168
167
|
file_path (PathLike): Path to the GIF file.
|
|
169
|
-
canvas_size (int): Size of the
|
|
168
|
+
canvas_size (int | Tuple[int, int]): Size of the device's canvas in pixels, one number for a square
|
|
169
|
+
canvas or (width, height).
|
|
170
170
|
resize_mode (ResizeMode): The mode to resize the image. Options are:
|
|
171
171
|
- ResizeMode.FIT: Resize to fit within the canvas while maintaining aspect ratio.
|
|
172
172
|
- ResizeMode.FILL: Resize to fill the canvas, may crop the image.
|
|
@@ -183,6 +183,7 @@ class GifModule(IDotMatrixModule):
|
|
|
183
183
|
Returns:
|
|
184
184
|
bytes: A byte representation of the GIF file, adapted to fit the pixel size.
|
|
185
185
|
"""
|
|
186
|
+
canvas_size = image_utils.canvas_dimensions(canvas_size)
|
|
186
187
|
from PIL import GifImagePlugin
|
|
187
188
|
GifImagePlugin.LOADING_STRATEGY = GifImagePlugin.LoadingStrategy.RGB_AFTER_DIFFERENT_PALETTE_ONLY
|
|
188
189
|
|
|
@@ -196,7 +197,7 @@ class GifModule(IDotMatrixModule):
|
|
|
196
197
|
source_durations.append(img.info.get("duration"))
|
|
197
198
|
frame = img.copy()
|
|
198
199
|
|
|
199
|
-
if frame.size !=
|
|
200
|
+
if frame.size != canvas_size:
|
|
200
201
|
# needs to use NEAREST to to avoid color distortion
|
|
201
202
|
resample_mode = PILImage.Resampling.NEAREST
|
|
202
203
|
frame = image_utils.resize_image(
|
|
@@ -85,7 +85,7 @@ class ImageModule(IDotMatrixModule):
|
|
|
85
85
|
pixel_data = await asyncio.to_thread(
|
|
86
86
|
self._load_image_and_adapt_to_canvas,
|
|
87
87
|
file_path=file_path,
|
|
88
|
-
canvas_size=self.screen_size.value
|
|
88
|
+
canvas_size=self.screen_size.value,
|
|
89
89
|
resize_mode=resize_mode,
|
|
90
90
|
palletize=palletize,
|
|
91
91
|
background_color=background_color,
|
|
@@ -95,7 +95,7 @@ class ImageModule(IDotMatrixModule):
|
|
|
95
95
|
@staticmethod
|
|
96
96
|
def _load_image_and_adapt_to_canvas(
|
|
97
97
|
file_path: PathLike | str,
|
|
98
|
-
canvas_size: int,
|
|
98
|
+
canvas_size: int | Tuple[int, int],
|
|
99
99
|
resize_mode: image_utils.ResizeMode,
|
|
100
100
|
palletize: bool,
|
|
101
101
|
background_color: Tuple[int, int, int],
|
|
@@ -103,17 +103,18 @@ class ImageModule(IDotMatrixModule):
|
|
|
103
103
|
overlay: TextOverlay | None = None,
|
|
104
104
|
) -> bytearray:
|
|
105
105
|
"""
|
|
106
|
-
Loads an image from a file, resizes it to
|
|
106
|
+
Loads an image from a file, resizes it to the size of the canvas,
|
|
107
107
|
and applies a black background if the image is smaller than the canvas size.
|
|
108
108
|
Args:
|
|
109
109
|
file_path (str): Path to the image file.
|
|
110
|
-
canvas_size (int): Size of the
|
|
110
|
+
canvas_size (int | Tuple[int, int]): Size of the canvas in pixels, one number for a square canvas or
|
|
111
|
+
(width, height).
|
|
111
112
|
palletize (bool): If True, the image will be converted to a palette-based image.
|
|
112
113
|
background_color (Tuple[int, int, int]): RGB color for the background, which is only visible if the input
|
|
113
114
|
colors (int): The number of colors of the palette that `palletize` reduces the image to.
|
|
114
115
|
overlay (TextOverlay, optional): A text that is drawn on top of the image.
|
|
115
116
|
Returns:
|
|
116
|
-
bytearray: A bytearray containing the raw pixel data of the processed image in RGB format.
|
|
117
|
+
bytearray: A bytearray containing the raw pixel data of the processed image in RGB format, row by row.
|
|
117
118
|
"""
|
|
118
119
|
if background_color is None or len(background_color) != 3:
|
|
119
120
|
raise ValueError("background_color must be a tuple of three integers (R, G, B)")
|
|
@@ -199,7 +200,7 @@ class ImageModule(IDotMatrixModule):
|
|
|
199
200
|
pixel_data = await asyncio.to_thread(
|
|
200
201
|
self._load_image_and_adapt_to_canvas,
|
|
201
202
|
file_path=file_path,
|
|
202
|
-
canvas_size=self.screen_size.value
|
|
203
|
+
canvas_size=self.screen_size.value,
|
|
203
204
|
resize_mode=resize_mode,
|
|
204
205
|
palletize=palletize,
|
|
205
206
|
background_color=background_color,
|
|
@@ -232,8 +233,7 @@ class ImageModule(IDotMatrixModule):
|
|
|
232
233
|
palette: If True, the PNG uses a palette of up to 256 colors, which is smaller, but changes the colors of
|
|
233
234
|
images with more than 256 of them.
|
|
234
235
|
"""
|
|
235
|
-
|
|
236
|
-
img = PILImage.frombytes("RGB", (size, size), bytes(pixel_data))
|
|
236
|
+
img = PILImage.frombytes("RGB", self.screen_size.value, bytes(pixel_data))
|
|
237
237
|
if palette:
|
|
238
238
|
img = img.quantize(colors, dither=PILImage.Dither.NONE)
|
|
239
239
|
buffer = io.BytesIO()
|
|
@@ -303,17 +303,17 @@ class ImageModule(IDotMatrixModule):
|
|
|
303
303
|
Args:
|
|
304
304
|
pixel_colors (List[Tuple[int, int, int]]): List of tuples representing RGB pixel values.
|
|
305
305
|
Each tuple should contain three integers (R, G, B) in the range 0-255.
|
|
306
|
-
|
|
306
|
+
One for each pixel of the screen (width * height), row by row.
|
|
307
307
|
"""
|
|
308
308
|
pixel_data = self._pixel_colors_to_bytes(pixel_colors)
|
|
309
309
|
|
|
310
310
|
return await self._send_diy_image_data(pixel_data)
|
|
311
311
|
|
|
312
312
|
def _pixel_colors_to_bytes(self, pixel_colors: List[Tuple[int, int, int] | int | str]) -> bytearray:
|
|
313
|
-
|
|
314
|
-
if len(pixel_colors) !=
|
|
313
|
+
width, height = self.screen_size.value
|
|
314
|
+
if len(pixel_colors) != width * height:
|
|
315
315
|
raise ValueError(
|
|
316
|
-
f"pixel_data must contain exactly {
|
|
316
|
+
f"pixel_data must contain exactly {width * height} pixels ({width}x{height}), got: {len(pixel_colors)}"
|
|
317
317
|
)
|
|
318
318
|
|
|
319
319
|
# parse the pixel_colors to ensure they are in RGB format
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ScreenSize(Enum):
|
|
5
|
+
"""
|
|
6
|
+
Enum for screen sizes. The value is (width, height) in pixels.
|
|
7
|
+
|
|
8
|
+
The names follow the device's own rows x columns naming, so SIZE_24x48 is 24 pixels high and 48 wide.
|
|
9
|
+
"""
|
|
10
|
+
SIZE_64x64 = (64, 64)
|
|
11
|
+
SIZE_32x32 = (32, 32)
|
|
12
|
+
SIZE_16x16 = (16, 16)
|
|
13
|
+
SIZE_24x48 = (48, 24)
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def width(self) -> int:
|
|
17
|
+
"""The number of pixels in a row."""
|
|
18
|
+
return self.value[0]
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def height(self) -> int:
|
|
22
|
+
"""The number of rows."""
|
|
23
|
+
return self.value[1]
|
|
@@ -158,7 +158,7 @@ class IDotMatrixApp(App):
|
|
|
158
158
|
self._client_factory = client_factory
|
|
159
159
|
self.client: IDotMatrixClient | None = None
|
|
160
160
|
self.connection = "connecting"
|
|
161
|
-
self.canvas_size = DEFAULT_CANVAS_SIZE
|
|
161
|
+
self.canvas_size = (DEFAULT_CANVAS_SIZE, DEFAULT_CANVAS_SIZE) # (width, height)
|
|
162
162
|
self.marked: list[Path] = []
|
|
163
163
|
self.text_filter = ""
|
|
164
164
|
self._busy: Worker | None = None
|
|
@@ -609,11 +609,12 @@ class IDotMatrixApp(App):
|
|
|
609
609
|
warning = self.main_screen.query_one("#preview-warning", Static)
|
|
610
610
|
except NoMatches: # the app is closing
|
|
611
611
|
return
|
|
612
|
-
|
|
612
|
+
(shown_width, shown_height), (actual_width, actual_height) = event.shown, event.actual
|
|
613
|
+
too_small = actual_width > shown_width > 0
|
|
613
614
|
warning.set_class(too_small, "visible")
|
|
614
615
|
if too_small:
|
|
615
|
-
warning.update(f"⚠ too small: drawn at {
|
|
616
|
-
"enlarge the terminal for an exact picture")
|
|
616
|
+
warning.update(f"⚠ too small: drawn at {shown_width}x{shown_height} of {actual_width}x{actual_height} "
|
|
617
|
+
"pixels, enlarge the terminal for an exact picture")
|
|
617
618
|
|
|
618
619
|
def _update_info(self) -> None:
|
|
619
620
|
path = self._previewed
|
|
@@ -638,7 +639,7 @@ class IDotMatrixApp(App):
|
|
|
638
639
|
text.append(symbols.get(self.connection, "○"), style=STATUS_COLORS.get(self.connection, "grey62"))
|
|
639
640
|
text.append(f" {self.connection}")
|
|
640
641
|
if self.connection == "connected":
|
|
641
|
-
text.append(f" {self.canvas_size}x{self.canvas_size}")
|
|
642
|
+
text.append(f" {self.canvas_size[0]}x{self.canvas_size[1]}")
|
|
642
643
|
if self.session.address:
|
|
643
644
|
known = self.session.device(self.session.address)
|
|
644
645
|
name = self._device_names.get(self.session.address) or (known.name if known else "")
|
|
@@ -660,7 +661,7 @@ class IDotMatrixApp(App):
|
|
|
660
661
|
self.client = self._client_factory(self.session.address)
|
|
661
662
|
await self._connect_client()
|
|
662
663
|
size = self.client.screen_size
|
|
663
|
-
self.canvas_size = size.value
|
|
664
|
+
self.canvas_size = size.value if isinstance(size, ScreenSize) else (DEFAULT_CANVAS_SIZE, DEFAULT_CANVAS_SIZE)
|
|
664
665
|
self.connection = "connected"
|
|
665
666
|
address = self.session.address
|
|
666
667
|
if address:
|
|
@@ -1,26 +1,34 @@
|
|
|
1
1
|
from PIL import Image as PILImage
|
|
2
2
|
|
|
3
|
+
from idotmatrix.util.image_utils import canvas_dimensions
|
|
4
|
+
|
|
3
5
|
Color = tuple[int, int, int]
|
|
4
6
|
BLACK: Color = (0, 0, 0)
|
|
5
7
|
MAX_UNDO = 100
|
|
6
8
|
|
|
7
9
|
|
|
8
10
|
class PixelCanvas:
|
|
9
|
-
"""A
|
|
11
|
+
"""A picture to draw on, with undo, and a record of what changed since it was last sent to the display."""
|
|
10
12
|
|
|
11
|
-
def __init__(self, size: int):
|
|
12
|
-
|
|
13
|
-
self.
|
|
13
|
+
def __init__(self, size: int | tuple[int, int]):
|
|
14
|
+
"""`size` is one number for a square canvas, or (width, height)."""
|
|
15
|
+
self.width, self.height = canvas_dimensions(size)
|
|
16
|
+
self._pixels: list[list[Color]] = [[BLACK] * self.width for _ in range(self.height)]
|
|
14
17
|
self._undo: list[list[list[Color]]] = []
|
|
15
18
|
self._redo: list[list[list[Color]]] = []
|
|
16
19
|
self._dirty: set[tuple[int, int]] = set()
|
|
17
20
|
self._touched: set[int] = set()
|
|
18
21
|
|
|
22
|
+
@property
|
|
23
|
+
def size(self) -> tuple[int, int]:
|
|
24
|
+
"""(width, height)"""
|
|
25
|
+
return self.width, self.height
|
|
26
|
+
|
|
19
27
|
def get(self, x: int, y: int) -> Color:
|
|
20
28
|
return self._pixels[y][x]
|
|
21
29
|
|
|
22
30
|
def inside(self, x: int, y: int) -> bool:
|
|
23
|
-
return 0 <= x < self.
|
|
31
|
+
return 0 <= x < self.width and 0 <= y < self.height
|
|
24
32
|
|
|
25
33
|
# changing
|
|
26
34
|
|
|
@@ -79,17 +87,17 @@ class PixelCanvas:
|
|
|
79
87
|
stack.extend(((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1)))
|
|
80
88
|
|
|
81
89
|
def clear(self) -> None:
|
|
82
|
-
for y in range(self.
|
|
83
|
-
for x in range(self.
|
|
90
|
+
for y in range(self.height):
|
|
91
|
+
for x in range(self.width):
|
|
84
92
|
self.set(x, y, BLACK)
|
|
85
93
|
|
|
86
94
|
def load(self, image: PILImage.Image) -> None:
|
|
87
95
|
"""Replaces the picture, scaled to the canvas without smoothing, so pixel art stays sharp."""
|
|
88
96
|
rgb = image.convert("RGB")
|
|
89
|
-
if rgb.size !=
|
|
90
|
-
rgb = rgb.resize(
|
|
91
|
-
for y in range(self.
|
|
92
|
-
for x in range(self.
|
|
97
|
+
if rgb.size != self.size:
|
|
98
|
+
rgb = rgb.resize(self.size, PILImage.Resampling.NEAREST)
|
|
99
|
+
for y in range(self.height):
|
|
100
|
+
for x in range(self.width):
|
|
93
101
|
self.set(x, y, rgb.getpixel((x, y)))
|
|
94
102
|
|
|
95
103
|
# undo
|
|
@@ -105,8 +113,8 @@ class PixelCanvas:
|
|
|
105
113
|
return False
|
|
106
114
|
target.append([row[:] for row in self._pixels])
|
|
107
115
|
state = source.pop()
|
|
108
|
-
for y in range(self.
|
|
109
|
-
for x in range(self.
|
|
116
|
+
for y in range(self.height):
|
|
117
|
+
for x in range(self.width):
|
|
110
118
|
self.set(x, y, state[y][x])
|
|
111
119
|
return True
|
|
112
120
|
|
|
@@ -126,12 +134,12 @@ class PixelCanvas:
|
|
|
126
134
|
|
|
127
135
|
def everything(self) -> dict[tuple[int, int], Color]:
|
|
128
136
|
self._dirty.clear()
|
|
129
|
-
return {(x, y): self._pixels[y][x] for y in range(self.
|
|
137
|
+
return {(x, y): self._pixels[y][x] for y in range(self.height) for x in range(self.width)}
|
|
130
138
|
|
|
131
139
|
# as a picture
|
|
132
140
|
|
|
133
141
|
def to_image(self) -> PILImage.Image:
|
|
134
|
-
image = PILImage.new("RGB",
|
|
142
|
+
image = PILImage.new("RGB", self.size)
|
|
135
143
|
image.putdata([pixel for row in self._pixels for pixel in row])
|
|
136
144
|
return image
|
|
137
145
|
|
|
@@ -36,13 +36,14 @@ def is_animated(path: Path) -> bool:
|
|
|
36
36
|
return image_utils.is_animated(img)
|
|
37
37
|
|
|
38
38
|
|
|
39
|
-
def convert(path: Path, canvas_size: int, options: Options) -> Preview:
|
|
39
|
+
def convert(path: Path, canvas_size: int | tuple[int, int], options: Options) -> Preview:
|
|
40
40
|
"""
|
|
41
41
|
Converts a file the way an upload does, with the options, and decodes the result. Slow for a long animation, so
|
|
42
|
-
not for the UI thread.
|
|
42
|
+
not for the UI thread. `canvas_size` is one number for a square screen, or (width, height).
|
|
43
43
|
"""
|
|
44
|
+
canvas_size = image_utils.canvas_dimensions(canvas_size)
|
|
45
|
+
screen = next((size for size in ScreenSize if size.value == canvas_size), ScreenSize.SIZE_64x64)
|
|
44
46
|
if is_animated(path):
|
|
45
|
-
screen = next((size for size in ScreenSize if size.value[0] == canvas_size), ScreenSize.SIZE_64x64)
|
|
46
47
|
module = GifModule(connection_manager=None, screen_size=screen)
|
|
47
48
|
data = module._load_gif_and_adapt_to_canvas(
|
|
48
49
|
file_path=path, canvas_size=canvas_size, resize_mode=options.resize,
|
|
@@ -63,9 +64,8 @@ def convert(path: Path, canvas_size: int, options: Options) -> Preview:
|
|
|
63
64
|
pixels = ImageModule._load_image_and_adapt_to_canvas(
|
|
64
65
|
file_path=path, canvas_size=canvas_size, resize_mode=options.resize, palletize=palletize,
|
|
65
66
|
background_color=options.background_rgb, colors=colors, overlay=options.overlay_for(path))
|
|
66
|
-
image = PILImage.frombytes("RGB",
|
|
67
|
+
image = PILImage.frombytes("RGB", canvas_size, bytes(pixels))
|
|
67
68
|
if options.png:
|
|
68
|
-
screen = next((size for size in ScreenSize if size.value[0] == canvas_size), ScreenSize.SIZE_64x64)
|
|
69
69
|
encoder = ImageModule(connection_manager=None, screen_size=screen)
|
|
70
70
|
size = len(encoder._encode_png(pixels, palette=palletize or colors < 256, colors=colors))
|
|
71
71
|
else:
|
|
@@ -70,7 +70,7 @@ class CanvasWidget(Widget):
|
|
|
70
70
|
def __init__(self, canvas: PixelCanvas, **kwargs):
|
|
71
71
|
super().__init__(**kwargs)
|
|
72
72
|
self.canvas = canvas
|
|
73
|
-
self.cursor = (canvas.
|
|
73
|
+
self.cursor = (canvas.width // 2, canvas.height // 2)
|
|
74
74
|
self.anchor: tuple[int, int] | None = None
|
|
75
75
|
self.wide = False
|
|
76
76
|
self.on_pointer = None # called with (x, y, button, started)
|
|
@@ -81,10 +81,10 @@ class CanvasWidget(Widget):
|
|
|
81
81
|
self._shown_marks: set[int] = set()
|
|
82
82
|
|
|
83
83
|
def get_content_width(self, container: Size, viewport: Size) -> int:
|
|
84
|
-
return self.canvas.
|
|
84
|
+
return self.canvas.width * (2 if self.wide else 1)
|
|
85
85
|
|
|
86
86
|
def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
|
|
87
|
-
return self.canvas.
|
|
87
|
+
return self.canvas.height if self.wide else (self.canvas.height + 1) // 2
|
|
88
88
|
|
|
89
89
|
def set_wide(self, wide: bool) -> None:
|
|
90
90
|
if wide != self.wide:
|
|
@@ -112,13 +112,13 @@ class CanvasWidget(Widget):
|
|
|
112
112
|
"""One row of cells. The rows are kept until a pixel in them changes, so a stroke draws a few of them."""
|
|
113
113
|
strip = self._strips.get(row)
|
|
114
114
|
if strip is None:
|
|
115
|
-
|
|
115
|
+
width, height = self.canvas.size
|
|
116
116
|
if self.wide:
|
|
117
117
|
segments = [Segment(" ", cell_style(self._color_at(x, row), self._color_at(x, row)))
|
|
118
|
-
for x in range(
|
|
118
|
+
for x in range(width)] if row < height else []
|
|
119
119
|
else:
|
|
120
120
|
segments = [Segment("▀", cell_style(self._color_at(x, row * 2), self._color_at(x, row * 2 + 1)))
|
|
121
|
-
for x in range(
|
|
121
|
+
for x in range(width)] if row * 2 < height else []
|
|
122
122
|
strip = self._strips[row] = Strip(segments)
|
|
123
123
|
return strip
|
|
124
124
|
|
|
@@ -257,7 +257,7 @@ class DrawScreen(PageScreen):
|
|
|
257
257
|
self._widget.on_pointer = self._pointer
|
|
258
258
|
with Horizontal(id="draw"):
|
|
259
259
|
with Vertical(id="canvas-box") as box:
|
|
260
|
-
box.border_title = f"Canvas {self.canvas.
|
|
260
|
+
box.border_title = f"Canvas {self.canvas.width}x{self.canvas.height}"
|
|
261
261
|
yield self._widget
|
|
262
262
|
with Vertical(id="side"):
|
|
263
263
|
with Vertical(id="palette-box") as palette:
|
|
@@ -290,9 +290,9 @@ class DrawScreen(PageScreen):
|
|
|
290
290
|
if self.canvas.size != self.app.canvas_size: # another display than the canvas was made for
|
|
291
291
|
self.canvas = PixelCanvas(self.app.canvas_size)
|
|
292
292
|
self._widget.canvas = self.canvas
|
|
293
|
-
self._widget.cursor = (self.canvas.
|
|
293
|
+
self._widget.cursor = (self.canvas.width // 2, self.canvas.height // 2)
|
|
294
294
|
self._widget.reset()
|
|
295
|
-
self.query_one("#canvas-box").border_title = f"Canvas {self.canvas.
|
|
295
|
+
self.query_one("#canvas-box").border_title = f"Canvas {self.canvas.width}x{self.canvas.height}"
|
|
296
296
|
self._diy_active = False
|
|
297
297
|
self._fit()
|
|
298
298
|
self._update_palette()
|
|
@@ -320,8 +320,8 @@ class DrawScreen(PageScreen):
|
|
|
320
320
|
box = self.query_one("#canvas-box")
|
|
321
321
|
except NoMatches:
|
|
322
322
|
return
|
|
323
|
-
|
|
324
|
-
self._widget.set_wide(box.size.width - 2 >=
|
|
323
|
+
width, height = self.canvas.size
|
|
324
|
+
self._widget.set_wide(box.size.width - 2 >= width * 2 and box.size.height - 2 >= height)
|
|
325
325
|
|
|
326
326
|
# drawing
|
|
327
327
|
|
|
@@ -376,8 +376,8 @@ class DrawScreen(PageScreen):
|
|
|
376
376
|
|
|
377
377
|
def action_move(self, dx: int, dy: int) -> None:
|
|
378
378
|
x, y = self._widget.cursor
|
|
379
|
-
|
|
380
|
-
self._widget.cursor = (min(max(x + dx, 0),
|
|
379
|
+
width, height = self.canvas.size
|
|
380
|
+
self._widget.cursor = (min(max(x + dx, 0), width - 1), min(max(y + dy, 0), height - 1))
|
|
381
381
|
if self.pen_down:
|
|
382
382
|
self._paint(*self._widget.cursor)
|
|
383
383
|
self._changed()
|
|
@@ -53,9 +53,18 @@ def columns_per_pixel(aspect: float) -> int:
|
|
|
53
53
|
return 2 if aspect >= WIDE_PIXEL_ASPECT else 1
|
|
54
54
|
|
|
55
55
|
|
|
56
|
-
def
|
|
57
|
-
"""
|
|
58
|
-
|
|
56
|
+
def fit_within(width: int, height: int, max_width: int, max_height: int) -> tuple[int, int]:
|
|
57
|
+
"""The size of a picture scaled down (never up) to fit in the space, keeping its aspect ratio."""
|
|
58
|
+
if width <= max_width and height <= max_height:
|
|
59
|
+
return width, height
|
|
60
|
+
if max_width * height <= max_height * width: # the width is what limits it
|
|
61
|
+
return max(1, max_width), max(1, height * max_width // width)
|
|
62
|
+
return max(1, width * max_height // height), max(1, max_height)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def fitted_size(image_size: tuple[int, int], max_columns: int, max_rows: int, aspect: float = 2.0) -> tuple[int, int]:
|
|
66
|
+
"""How many pixels (width, height) of the picture fit in the space."""
|
|
67
|
+
return fit_within(*image_size, max_columns // columns_per_pixel(aspect), max_rows * 2)
|
|
59
68
|
|
|
60
69
|
|
|
61
70
|
def render_frame(image: PILImage.Image, max_columns: int, max_rows: int, aspect: float = 2.0) -> Text:
|
|
@@ -65,29 +74,32 @@ def render_frame(image: PILImage.Image, max_columns: int, max_rows: int, aspect:
|
|
|
65
74
|
`detect_pixel_image` for the exact way.
|
|
66
75
|
"""
|
|
67
76
|
per_pixel = columns_per_pixel(aspect)
|
|
68
|
-
|
|
69
|
-
if
|
|
70
|
-
image = image.resize((
|
|
77
|
+
width, height = fitted_size(image.size, max_columns, max_rows, aspect)
|
|
78
|
+
if (width, height) != image.size:
|
|
79
|
+
image = image.resize((width, height), PILImage.Resampling.NEAREST)
|
|
71
80
|
image = image.convert("RGB")
|
|
72
81
|
pixels = image.load()
|
|
73
|
-
columns =
|
|
82
|
+
columns = width * per_pixel
|
|
74
83
|
text = Text(no_wrap=True, overflow="crop")
|
|
75
|
-
for y in range(0,
|
|
84
|
+
for y in range(0, height, 2):
|
|
76
85
|
for column in range(columns):
|
|
77
86
|
x = column // per_pixel
|
|
78
87
|
top = pixels[x, y]
|
|
79
|
-
bottom = pixels[x, y + 1] if y + 1 <
|
|
88
|
+
bottom = pixels[x, y + 1] if y + 1 < height else (0, 0, 0)
|
|
80
89
|
text.append(HALF_BLOCK, Style(color=f"rgb({top[0]},{top[1]},{top[2]})",
|
|
81
90
|
bgcolor=f"rgb({bottom[0]},{bottom[1]},{bottom[2]})"))
|
|
82
|
-
if y + 2 <
|
|
91
|
+
if y + 2 < height:
|
|
83
92
|
text.append("\n")
|
|
84
93
|
return text
|
|
85
94
|
|
|
86
95
|
|
|
87
96
|
class Fitted(Message):
|
|
88
|
-
"""
|
|
97
|
+
"""
|
|
98
|
+
The picture was drawn with `shown` (width, height) pixels of the `actual` ones (equal if it fits, (0, 0) if there
|
|
99
|
+
is no picture).
|
|
100
|
+
"""
|
|
89
101
|
|
|
90
|
-
def __init__(self, shown: int, actual: int):
|
|
102
|
+
def __init__(self, shown: tuple[int, int], actual: tuple[int, int]):
|
|
91
103
|
super().__init__()
|
|
92
104
|
self.shown = shown
|
|
93
105
|
self.actual = actual
|
|
@@ -164,13 +176,13 @@ class PreviewWidget(_Player, Static):
|
|
|
164
176
|
self._stop()
|
|
165
177
|
self._frames, self._durations = [], []
|
|
166
178
|
self.update(message)
|
|
167
|
-
self.post_message(Fitted(0, 0))
|
|
179
|
+
self.post_message(Fitted((0, 0), (0, 0)))
|
|
168
180
|
|
|
169
181
|
def _draw(self) -> None:
|
|
170
182
|
size = self.size
|
|
171
183
|
if size.width and size.height:
|
|
172
|
-
actual = self._frames[self._index].
|
|
173
|
-
self.post_message(Fitted(
|
|
184
|
+
actual = self._frames[self._index].size
|
|
185
|
+
self.post_message(Fitted(fitted_size(actual, size.width, size.height, self._aspect), actual))
|
|
174
186
|
self.update(render_frame(self._frames[self._index], size.width, size.height, self._aspect))
|
|
175
187
|
|
|
176
188
|
|
|
@@ -225,7 +237,7 @@ class PixelPreviewWidget(_Player, Vertical):
|
|
|
225
237
|
self._pixels.image = None
|
|
226
238
|
self._label.display = True
|
|
227
239
|
self._label.update(message)
|
|
228
|
-
self.post_message(Fitted(0, 0))
|
|
240
|
+
self.post_message(Fitted((0, 0), (0, 0)))
|
|
229
241
|
|
|
230
242
|
def _draw(self) -> None:
|
|
231
243
|
size = self.size
|
|
@@ -234,10 +246,10 @@ class PixelPreviewWidget(_Player, Vertical):
|
|
|
234
246
|
from textual_image.widget import get_cell_size
|
|
235
247
|
cell = get_cell_size()
|
|
236
248
|
frame = self._frames[self._index]
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
self.post_message(Fitted(min(side, room), side))
|
|
249
|
+
room_width, room_height = size.width * cell.width, size.height * cell.height
|
|
250
|
+
factor = max(1, min(room_width // frame.width, room_height // frame.height))
|
|
251
|
+
self.post_message(Fitted(fit_within(frame.width, frame.height, room_width, room_height), frame.size))
|
|
241
252
|
self._label.display = False
|
|
242
253
|
self._pixels.display = True
|
|
243
|
-
self._pixels.image = frame.convert("RGB").resize(
|
|
254
|
+
self._pixels.image = frame.convert("RGB").resize(
|
|
255
|
+
(frame.width * factor, frame.height * factor), PILImage.Resampling.NEAREST)
|
|
@@ -109,7 +109,7 @@ class TextPreview(Static):
|
|
|
109
109
|
|
|
110
110
|
def show(self, values: dict) -> None:
|
|
111
111
|
try:
|
|
112
|
-
columns = self.app.canvas_size
|
|
112
|
+
columns = self.app.canvas_size[0]
|
|
113
113
|
text, note = render_text(values["text"], values["font_size"], values["color_mode"], values["color"],
|
|
114
114
|
values.get("background"), columns)
|
|
115
115
|
except Exception as e: # a font size the font cannot draw, an empty text
|
|
@@ -78,6 +78,23 @@ def pad_palette(image: PILImage.Image, size: int = 256) -> PILImage.Image:
|
|
|
78
78
|
return image
|
|
79
79
|
|
|
80
80
|
|
|
81
|
+
def canvas_dimensions(canvas_size: int | tuple[int, int]) -> tuple[int, int]:
|
|
82
|
+
"""
|
|
83
|
+
The (width, height) of a canvas, given as one number for a square canvas or as (width, height).
|
|
84
|
+
|
|
85
|
+
:raises ValueError: if the size is not a positive number or a pair of them.
|
|
86
|
+
"""
|
|
87
|
+
if isinstance(canvas_size, int) and not isinstance(canvas_size, bool):
|
|
88
|
+
dimensions = (canvas_size, canvas_size)
|
|
89
|
+
elif isinstance(canvas_size, (tuple, list)) and len(canvas_size) == 2:
|
|
90
|
+
dimensions = tuple(canvas_size)
|
|
91
|
+
else:
|
|
92
|
+
raise ValueError(f"canvas_size must be a number or (width, height), got: {canvas_size!r}")
|
|
93
|
+
if not all(isinstance(side, int) and not isinstance(side, bool) and side > 0 for side in dimensions):
|
|
94
|
+
raise ValueError(f"canvas_size must be positive, got: {canvas_size!r}")
|
|
95
|
+
return dimensions
|
|
96
|
+
|
|
97
|
+
|
|
81
98
|
class ResizeMode(Enum):
|
|
82
99
|
"""
|
|
83
100
|
Enum for resize modes.
|
|
@@ -89,7 +106,7 @@ class ResizeMode(Enum):
|
|
|
89
106
|
|
|
90
107
|
def resize_image(
|
|
91
108
|
image: PILImage.Image,
|
|
92
|
-
canvas_size: int,
|
|
109
|
+
canvas_size: int | tuple[int, int],
|
|
93
110
|
resize_mode: ResizeMode,
|
|
94
111
|
resample_mode: PILImage.Resampling,
|
|
95
112
|
background_color: tuple[int, int, int] = (0, 0, 0),
|
|
@@ -99,21 +116,22 @@ def resize_image(
|
|
|
99
116
|
Resize an image to a specific size.
|
|
100
117
|
|
|
101
118
|
:param image: The input image to be resized.
|
|
102
|
-
:param canvas_size: The
|
|
119
|
+
:param canvas_size: The size of the canvas to fit the image into, one number for a square canvas or (width, height).
|
|
103
120
|
:param resize_mode: The mode to use for resizing the image (ResizeMode.FIT, ResizeMode.FILL, ResizeMode.STRETCH).
|
|
104
121
|
:param resample_mode: The resampling mode to use for resizing (e.g., PILImage.Resampling.LANCZOS).
|
|
105
122
|
:param background_color: The color to fill the background with if the image does not fill the whole canvas.
|
|
106
123
|
:param mode: The mode to use for the new image (default is "RGB").
|
|
107
|
-
:return: The resized image.
|
|
124
|
+
:return: The resized image, exactly the size of the canvas.
|
|
108
125
|
"""
|
|
126
|
+
width, height = canvas_dimensions(canvas_size)
|
|
109
127
|
if resize_mode == ResizeMode.FIT:
|
|
110
128
|
# if the dimensions of the frame are not equal to the pixel size, resize it while maintaining the aspect ratio
|
|
111
129
|
# and adding a black background if necessary.
|
|
112
|
-
ratio = min(
|
|
130
|
+
ratio = min(width / image.width, height / image.height)
|
|
113
131
|
# at least one pixel, a very wide or tall picture would be 0 pixels high on a small screen
|
|
114
132
|
new_size = (
|
|
115
|
-
min(
|
|
116
|
-
min(
|
|
133
|
+
min(width, max(1, round(image.width * ratio))),
|
|
134
|
+
min(height, max(1, round(image.height * ratio))),
|
|
117
135
|
)
|
|
118
136
|
image = image.resize(
|
|
119
137
|
size=new_size,
|
|
@@ -122,11 +140,11 @@ def resize_image(
|
|
|
122
140
|
elif resize_mode == ResizeMode.FILL:
|
|
123
141
|
# if the dimensions of the frame are not equal to the pixel size, resize it to fill the canvas
|
|
124
142
|
# this might crop the image, but maintains aspect ratio
|
|
125
|
-
ratio = max(
|
|
143
|
+
ratio = max(width / image.width, height / image.height)
|
|
126
144
|
# never smaller than the canvas, rounding down would leave a stripe of background
|
|
127
145
|
new_size = (
|
|
128
|
-
max(
|
|
129
|
-
max(
|
|
146
|
+
max(width, round(image.width * ratio)),
|
|
147
|
+
max(height, round(image.height * ratio)),
|
|
130
148
|
)
|
|
131
149
|
image = image.resize(
|
|
132
150
|
size=new_size,
|
|
@@ -134,40 +152,40 @@ def resize_image(
|
|
|
134
152
|
)
|
|
135
153
|
# crop anything that is outside the canvas size
|
|
136
154
|
box = (
|
|
137
|
-
(image.width -
|
|
138
|
-
(image.height -
|
|
139
|
-
(image.width +
|
|
140
|
-
(image.height +
|
|
155
|
+
(image.width - width) // 2,
|
|
156
|
+
(image.height - height) // 2,
|
|
157
|
+
(image.width + width) // 2,
|
|
158
|
+
(image.height + height) // 2,
|
|
141
159
|
)
|
|
142
160
|
image = image.crop(box)
|
|
143
161
|
elif resize_mode == ResizeMode.STRETCH:
|
|
144
162
|
# if the dimensions of the frame are not equal to the pixel size, stretch it to fit the canvas
|
|
145
163
|
# and add a black background if necessary.
|
|
146
164
|
image = image.resize(
|
|
147
|
-
size=(
|
|
165
|
+
size=(width, height),
|
|
148
166
|
resample=resample_mode,
|
|
149
167
|
)
|
|
150
168
|
|
|
151
169
|
# convert transparent pixels to the background color
|
|
152
170
|
new_image = PILImage.new(
|
|
153
171
|
mode="RGBA",
|
|
154
|
-
size=(
|
|
172
|
+
size=(width, height),
|
|
155
173
|
color=background_color
|
|
156
174
|
)
|
|
157
175
|
new_image.paste(
|
|
158
176
|
im=image,
|
|
159
|
-
box=((
|
|
177
|
+
box=((width - image.width) // 2, (height - image.height) // 2),
|
|
160
178
|
mask=image.convert("RGBA")
|
|
161
179
|
)
|
|
162
180
|
image = new_image
|
|
163
181
|
|
|
164
182
|
# ensure exact image dimensions
|
|
165
|
-
# ensure the image is always exactly
|
|
183
|
+
# ensure the image is always exactly width x height pixels and
|
|
166
184
|
# fill the background behind the image with background_color, if the image doesn't fill the whole canvas
|
|
167
|
-
new_img = PILImage.new(mode, (
|
|
185
|
+
new_img = PILImage.new(mode, (width, height), background_color)
|
|
168
186
|
new_img.paste(
|
|
169
187
|
im=image,
|
|
170
|
-
box=((
|
|
188
|
+
box=((width - image.width) // 2, (height - image.height) // 2)
|
|
171
189
|
)
|
|
172
190
|
image = new_img
|
|
173
191
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/apps/digital_picture_frame.py
RENAMED
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/connection_manager.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-PixelOperator8.ttf
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogicabold.ttf
RENAMED
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogicapixel.ttf
RENAMED
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogicapixelbold.ttf
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/32-Jersey10-Regular.ttf
RENAMED
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/ErbosDraco-License.txt
RENAMED
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/HomeVideo-License.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/Rain-DRM3-License.txt
RENAMED
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/dogica_license.txt
RENAMED
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/chronograph.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/fullscreen_color.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/music_sync.py
RENAMED
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/scoreboard.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{idotmatrix_api_client-1.1.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/device_material.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|