f
This commit is contained in:
@@ -11,7 +11,7 @@ from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import sys
|
||||
from typing import Any, Iterator, TextIO
|
||||
from typing import Any, Iterator, TextIO, List, Dict, Optional, Tuple, cast
|
||||
|
||||
from rich.console import Console
|
||||
from rich.panel import Panel
|
||||
@@ -200,8 +200,8 @@ def render_image_to_console(image_path: str | Path, max_width: int | None = None
|
||||
if not path.exists() or not path.is_file():
|
||||
return
|
||||
|
||||
with Image.open(path) as img:
|
||||
img = img.convert("RGB")
|
||||
with Image.open(path) as opened_img:
|
||||
img = opened_img.convert("RGB")
|
||||
orig_w, orig_h = img.size
|
||||
|
||||
# Determine target dimensions
|
||||
@@ -235,14 +235,21 @@ def render_image_to_console(image_path: str | Path, max_width: int | None = None
|
||||
|
||||
img = img.resize((target_w, target_h), Image.Resampling.BILINEAR)
|
||||
pixels = img.load()
|
||||
if pixels is None:
|
||||
return
|
||||
|
||||
# Render using upper half block (U+2580)
|
||||
# Each character row in terminal represents 2 pixel rows in image.
|
||||
for y in range(0, target_h - 1, 2):
|
||||
line = Text()
|
||||
for x in range(target_w):
|
||||
r1, g1, b1 = pixels[x, y]
|
||||
r2, g2, b2 = pixels[x, y + 1]
|
||||
rgb1 = cast(tuple, pixels[x, y])
|
||||
rgb2 = cast(tuple, pixels[x, y + 1])
|
||||
try:
|
||||
r1, g1, b1 = int(rgb1[0]), int(rgb1[1]), int(rgb1[2])
|
||||
r2, g2, b2 = int(rgb2[0]), int(rgb2[1]), int(rgb2[2])
|
||||
except Exception:
|
||||
r1 = g1 = b1 = r2 = g2 = b2 = 0
|
||||
# Foreground is top pixel, background is bottom pixel
|
||||
line.append(
|
||||
"▀",
|
||||
|
||||
Reference in New Issue
Block a user