Skip to content

Commit

Permalink
Merge pull request #5 from murilo-cunha/support-37
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
darrenburns authored Nov 10, 2022
2 parents 226be38 + fd0c3b3 commit ae8e5ee
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 15 deletions.
95 changes: 91 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ readme = "README.md"
packages = [{ include = "rich_pixels" }]

[tool.poetry.dependencies]
python = "^3.8"
python = "^3.7"
rich = "^12.0.0"
pillow = "^9.0.0"

[tool.poetry.group.dev.dependencies]
black = "^22.10.0"
mypy = "^0.990"
syrupy = "^3.0.5"
importlib-metadata = "^5.0.0"
types-pillow = "^9.3.0.1"

[build-system]
requires = ["poetry-core"]
Expand Down
23 changes: 13 additions & 10 deletions rich_pixels/_pixel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

from pathlib import Path, PurePath
from typing import Iterable, Mapping
from typing import Iterable, Mapping, Tuple, Union, Optional, List

from PIL import Image
from PIL import Image as PILImageModule
from PIL.Image import Image
from PIL.Image import Resampling
from rich.console import Console, ConsoleOptions, RenderResult
from rich.segment import Segment, Segments
Expand All @@ -16,30 +17,30 @@ def __init__(self) -> None:

@staticmethod
def from_image(
image: "Image",
image: Image,
):
segments = Pixels._segments_from_image(image)
return Pixels.from_segments(segments)

@staticmethod
def from_image_path(
path: PurePath | str,
resize: tuple[int, int] | None = None,
path: Union[PurePath, str],
resize: Optional[Tuple[int, int]] = None,
) -> Pixels:
"""Create a Pixels object from an image. Requires 'image' extra dependencies.
Args:
path: The path to the image file.
resize: A tuple of (width, height) to resize the image to.
"""
with Image.open(Path(path)) as image:
with PILImageModule.open(Path(path)) as image:
segments = Pixels._segments_from_image(image, resize)

return Pixels.from_segments(segments)

@staticmethod
def _segments_from_image(
image: "Image", resize: tuple[int, int] | None = None
image: Image, resize: Optional[Tuple[int, int]] = None
) -> list[Segment]:
if resize:
image = image.resize(resize, resample=Resampling.NEAREST)
Expand All @@ -52,7 +53,7 @@ def _segments_from_image(
segments = []

for y in range(height):
this_row = []
this_row: List[Segment] = []
row_append = this_row.append

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

@staticmethod
def from_ascii(grid: str, mapping: Mapping[str, Segment] | None = None) -> Pixels:
def from_ascii(
grid: str, mapping: Optional[Mapping[str, Segment]] = None
) -> Pixels:
"""
Create a Pixels object from a 2D-grid of ASCII characters.
Each ASCII character can be mapped to a Segment (a character and style combo),
Expand Down Expand Up @@ -108,7 +111,7 @@ def __rich_console__(
yield self._segments or ""


if __name__ == '__main__':
if __name__ == "__main__":
console = Console()
images_path = Path(__file__).parent / "../tests/.sample_data/images"
pixels = Pixels.from_image_path(images_path / "bulbasaur.png")
Expand Down

0 comments on commit ae8e5ee

Please sign in to comment.