From e5192c9e9deacae0a30baf6cb30994eef055ae77 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Sat, 10 Feb 2024 20:50:35 +0000 Subject: [PATCH] Minor changes --- pyproject.toml | 2 +- rich_pixels/_pixel.py | 26 ++++++++++++++++++++------ rich_pixels/_renderer.py | 7 ++++--- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 57f1e07..49cd276 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,4 +30,4 @@ dev-dependencies = [ allow-direct-references = true [tool.hatch.build.targets.wheel] -packages = ["src/rich_pixels"] \ No newline at end of file +packages = ["src/rich_pixels"] diff --git a/rich_pixels/_pixel.py b/rich_pixels/_pixel.py index 03cd487..5226ca2 100644 --- a/rich_pixels/_pixel.py +++ b/rich_pixels/_pixel.py @@ -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) @@ -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 diff --git a/rich_pixels/_renderer.py b/rich_pixels/_renderer.py index 43257cf..7b35d78 100644 --- a/rich_pixels/_renderer.py +++ b/rich_pixels/_renderer.py @@ -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] @@ -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. """ @@ -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: