Skip to content

Commit ae8e5ee

Browse files
authored
Merge pull request #5 from murilo-cunha/support-37
Closes #2
2 parents 226be38 + fd0c3b3 commit ae8e5ee

File tree

3 files changed

+107
-15
lines changed

3 files changed

+107
-15
lines changed

poetry.lock

+91-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ readme = "README.md"
77
packages = [{ include = "rich_pixels" }]
88

99
[tool.poetry.dependencies]
10-
python = "^3.8"
10+
python = "^3.7"
1111
rich = "^12.0.0"
1212
pillow = "^9.0.0"
1313

1414
[tool.poetry.group.dev.dependencies]
1515
black = "^22.10.0"
1616
mypy = "^0.990"
1717
syrupy = "^3.0.5"
18+
importlib-metadata = "^5.0.0"
19+
types-pillow = "^9.3.0.1"
1820

1921
[build-system]
2022
requires = ["poetry-core"]

rich_pixels/_pixel.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
22

33
from pathlib import Path, PurePath
4-
from typing import Iterable, Mapping
4+
from typing import Iterable, Mapping, Tuple, Union, Optional, List
55

6-
from PIL import Image
6+
from PIL import Image as PILImageModule
7+
from PIL.Image import Image
78
from PIL.Image import Resampling
89
from rich.console import Console, ConsoleOptions, RenderResult
910
from rich.segment import Segment, Segments
@@ -16,30 +17,30 @@ def __init__(self) -> None:
1617

1718
@staticmethod
1819
def from_image(
19-
image: "Image",
20+
image: Image,
2021
):
2122
segments = Pixels._segments_from_image(image)
2223
return Pixels.from_segments(segments)
2324

2425
@staticmethod
2526
def from_image_path(
26-
path: PurePath | str,
27-
resize: tuple[int, int] | None = None,
27+
path: Union[PurePath, str],
28+
resize: Optional[Tuple[int, int]] = None,
2829
) -> Pixels:
2930
"""Create a Pixels object from an image. Requires 'image' extra dependencies.
3031
3132
Args:
3233
path: The path to the image file.
3334
resize: A tuple of (width, height) to resize the image to.
3435
"""
35-
with Image.open(Path(path)) as image:
36+
with PILImageModule.open(Path(path)) as image:
3637
segments = Pixels._segments_from_image(image, resize)
3738

3839
return Pixels.from_segments(segments)
3940

4041
@staticmethod
4142
def _segments_from_image(
42-
image: "Image", resize: tuple[int, int] | None = None
43+
image: Image, resize: Optional[Tuple[int, int]] = None
4344
) -> list[Segment]:
4445
if resize:
4546
image = image.resize(resize, resample=Resampling.NEAREST)
@@ -52,7 +53,7 @@ def _segments_from_image(
5253
segments = []
5354

5455
for y in range(height):
55-
this_row = []
56+
this_row: List[Segment] = []
5657
row_append = this_row.append
5758

5859
for x in range(width):
@@ -78,7 +79,9 @@ def from_segments(
7879
return pixels
7980

8081
@staticmethod
81-
def from_ascii(grid: str, mapping: Mapping[str, Segment] | None = None) -> Pixels:
82+
def from_ascii(
83+
grid: str, mapping: Optional[Mapping[str, Segment]] = None
84+
) -> Pixels:
8285
"""
8386
Create a Pixels object from a 2D-grid of ASCII characters.
8487
Each ASCII character can be mapped to a Segment (a character and style combo),
@@ -108,7 +111,7 @@ def __rich_console__(
108111
yield self._segments or ""
109112

110113

111-
if __name__ == '__main__':
114+
if __name__ == "__main__":
112115
console = Console()
113116
images_path = Path(__file__).parent / "../tests/.sample_data/images"
114117
pixels = Pixels.from_image_path(images_path / "bulbasaur.png")

0 commit comments

Comments
 (0)