Skip to content

Commit

Permalink
Update 2.2.0 (RELEASE)
Browse files Browse the repository at this point in the history
  • Loading branch information
romanin-rf committed May 9, 2023
1 parent 4b88483 commit 48e2983
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[tool.poetry]
name = "ripix"
version = "2.2.0"
version = "2.2.1"
description = "A Rich-compatible library for writing pixel images and ASCII art to the terminal."
authors = ["Darren Burns <[email protected]>", "Romanin <[email protected]>"]
repository = "https://github.com/romanin-rf/ripix"
keywords = ["ripix", "rich", "textual", "image", "ascii-art", "rich_pixels"]
readme = "README.md"
packages = [{ include = "ripix" }]
include = ["ripix/py.typed", "ripix/functions.pyi"]
Expand Down
4 changes: 2 additions & 2 deletions ripix/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
async def aiter(it):
for item in it:
yield item
asyncio.sleep(0)
await asyncio.sleep(0)

async def arange(*args, **kwargs) -> int:
for i in range(*args, **kwargs):
yield i
asyncio.sleep(0)
await asyncio.sleep(0)

async def run_in_executor(loop, executor, func, *args, **kwargs):
if loop is None: loop = asyncio.get_running_loop()
Expand Down
28 changes: 15 additions & 13 deletions ripix/pixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# > Local Imports
from .functions import arange, aiter, wrapper_run_in_executor, run_in_executor


# ! Just Pixels
class Pixels:
def __init__(self) -> None:
Expand Down Expand Up @@ -66,13 +65,12 @@ def _segments_from_image(
for x in range(width):
r, g, b, a = get_pixel((x, y))
style = parse_style(f"on rgb({r},{g},{b})") if a > 0 else null_style
row_append(Segment(" ", style))
row_append(Segment(" ", style))

row_append(Segment("\n", null_style))

# TODO: Double-check if this is required - I've forgotten...
if not all(t[1] == "" for t in this_row[:-1]):
segments += this_row
segments += this_row

return segments

Expand Down Expand Up @@ -128,17 +126,20 @@ def __init__(self) -> None:
async def from_image(
image: Image,
resize: Optional[Tuple[int, int]] = None,
resample: Optional[Resampling] = None,
loop: Optional[AbstractEventLoop] = None
) -> AsyncPixels:
if loop is None: loop = get_running_loop()
resample = resample or Resampling.NEAREST

segments = await AsyncPixels._segments_from_image(image, resize, loop)
segments = await AsyncPixels._segments_from_image(image, resize, resample, loop)
return AsyncPixels.from_segments(segments)

@staticmethod
async def from_image_path(
path: Union[PurePath, str],
resize: Optional[Tuple[int, int]] = None,
resample: Optional[Resampling] = None,
loop: Optional[AbstractEventLoop] = None
) -> AsyncPixels:
"""Create a Pixels object from an image. Requires 'image' extra dependencies.
Expand All @@ -147,22 +148,26 @@ async def from_image_path(
path: The path to the image file.
resize: A tuple of (width, height) to resize the image to.
"""
resample = resample or Resampling.NEAREST

if loop is None: loop = get_running_loop()
pilopen = wrapper_run_in_executor(loop, None, PILImageModule.open)

with await pilopen(Path(path)) as image:
segments = await AsyncPixels._segments_from_image(image, resize, loop)
segments = await AsyncPixels._segments_from_image(image, resize, resample, loop)

return await AsyncPixels.from_segments(segments)

@staticmethod
async def _segments_from_image(
image: Image,
resize: Optional[Tuple[int, int]] = None,
resize_resample: Optional[Resampling] = None,
loop: Optional[AbstractEventLoop] = None
) -> List[Segment]:
resample = resize_resample or Resampling.NEAREST
if loop is None: loop = get_running_loop()
if resize is not None: image = await run_in_executor(loop, None, image.resize, resize, resample=Resampling.NEAREST)
if resize is not None: image = await run_in_executor(loop, None, image.resize, resize, resample=resample)

width, height = image.width, image.height
rgba_image = await run_in_executor(loop, None, image.convert, "RGBA") if image.mode != "RGBA" else image
Expand All @@ -178,13 +183,10 @@ async def _segments_from_image(
async for x in arange(width):
r, g, b, a = await get_pixel((x, y))
style = await parse_style(f"on rgb({r},{g},{b})") if (a > 0) else null_style
row_append(Segment(" ", style))

row_append(Segment(" ", style))
row_append(Segment("\n", null_style))

# TODO: Double-check if this is required - I've forgotten...
if not all(t[1] == "" async for t in aiter(this_row[:-1])):
segments += this_row

segments += this_row

return segments

Expand Down

0 comments on commit 48e2983

Please sign in to comment.