Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Feb 10, 2024
1 parent d3b486e commit e5192c9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ dev-dependencies = [
allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/rich_pixels"]
packages = ["src/rich_pixels"]
26 changes: 20 additions & 6 deletions rich_pixels/_pixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,35 @@ def __init__(self) -> None:
@staticmethod
def from_image(
image: Image,
renderer: Renderer = HalfcellRenderer(),
resize: Optional[Tuple[int, int]] = None,
renderer: Renderer | None = None,
):
segments = Pixels._segments_from_image(image, renderer=renderer)
"""Create a Pixels object from a PIL Image.
Requires 'image' extra dependencies.
Args:
image: The PIL Image
resize: A tuple of (width, height) to resize the image to.
renderer: The renderer to use. If None, the default half-cell renderer will
be used.
"""
segments = Pixels._segments_from_image(image, resize, renderer=renderer)
return Pixels.from_segments(segments)

@staticmethod
def from_image_path(
path: Union[PurePath, str],
resize: Optional[Tuple[int, int]] = None,
renderer: Renderer = HalfcellRenderer(),
renderer: Renderer | None = None,
) -> Pixels:
"""Create a Pixels object from an image. Requires 'image' extra dependencies.
"""Create a Pixels object from an image path.
Requires 'image' extra dependencies.
Args:
path: The path to the image file.
resize: A tuple of (width, height) to resize the image to.
renderer: The renderer to use. Defaults to HalfcellRenderer.
renderer: The renderer to use. If None, the default half-cell renderer will
be used.
"""
with PILImageModule.open(Path(path)) as image:
segments = Pixels._segments_from_image(image, resize, renderer=renderer)
Expand All @@ -46,8 +58,10 @@ def from_image_path(
def _segments_from_image(
image: Image,
resize: Optional[Tuple[int, int]] = None,
renderer: Renderer = HalfcellRenderer(),
renderer: Renderer | None = None,
) -> list[Segment]:
if renderer is None:
renderer = HalfcellRenderer()
return renderer.render(image, resize)

@staticmethod
Expand Down
7 changes: 4 additions & 3 deletions rich_pixels/_renderer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

from typing import Callable, Tuple

from PIL.Image import Image, Resampling
from rich.segment import Segment
from rich.style import Style
from PIL.Image import Image, Resampling

RGBA = Tuple[int, int, int, int]
GetPixel = Callable[[Tuple[int, int]], RGBA]
Expand Down Expand Up @@ -32,7 +33,7 @@ def __init__(
None if default_color is None else Style.parse(f"on {default_color}")
)

def render(self, image: Image, resize: Tuple[int, int] | None) -> list[Segment]:
def render(self, image: Image, resize: tuple[int, int] | None) -> list[Segment]:
"""
Render an image to Segments.
"""
Expand Down Expand Up @@ -80,7 +81,7 @@ class HalfcellRenderer(Renderer):
Render an image to half-height cells.
"""

def render(self, image: Image, resize: Tuple[int, int] | None) -> list[Segment]:
def render(self, image: Image, resize: tuple[int, int] | None) -> list[Segment]:
# because each row is 2 lines high, so we need to make sure the height is even
target_height = resize[1] if resize else image.size[1]
if target_height % 2 != 0:
Expand Down

0 comments on commit e5192c9

Please sign in to comment.