xnano.components.image
xnano.components.image
¶
xnano.components.image
Render images and animations directly into terminal cells.
Classes:
-
ImageFrame–One native-resolution RGB frame and its display duration.
-
ImageData–Decoded image frames independent of Pillow and the render engine.
-
Image–A native-resolution terminal image or real-time GIF component.
Attributes:
-
ImageFit(TypeAlias) –How source pixels are placed inside the available terminal area.
-
HorizontalPixelsPerCell(TypeAlias) –Number of adjacent source pixels sampled into each terminal cell.
-
ImageSource(TypeAlias) –A filesystem path, encoded image bytes, stream, or decoded image data.
ImageFit
module-attribute
¶
How source pixels are placed inside the available terminal area.
HorizontalPixelsPerCell
module-attribute
¶
Number of adjacent source pixels sampled into each terminal cell.
ImageSource
module-attribute
¶
A filesystem path, encoded image bytes, stream, or decoded image data.
ImageFrame
dataclass
¶
One native-resolution RGB frame and its display duration.
Attributes:
-
pixels(bytes) –Packed row-major RGB bytes.
-
duration_ms(int) –Display duration in milliseconds.
ImageData
dataclass
¶
ImageData(
width: int, height: int, frames: tuple[ImageFrame, ...]
)
Decoded image frames independent of Pillow and the render engine.
Attributes:
-
width(int) –Native pixel width shared by every frame.
-
height(int) –Native pixel height shared by every frame.
-
frames(tuple[ImageFrame, ...]) –RGB frames in playback order.
Methods:
-
from_bytes–Decode xnano's compact, Pillow-free animation container.
from_bytes
classmethod
¶
Decode xnano's compact, Pillow-free animation container.
Parameters:
-
data(bytes) –Bytes produced by
scripts/precompute_demo_image.py.
Returns:
-
ImageData–Decoded full-resolution RGB image data.
Source code in xnano/components/image.py
Image
dataclass
¶
Image(
source: ImageSource = "",
fit: ImageFit = "crop",
loop: bool = True,
speed: float = 1.0,
background: tuple[int, int, int] = (0, 0, 0),
horizontal_pixels_per_cell: HorizontalPixelsPerCell = 1,
correct_terminal_aspect: bool = False,
playing: bool = True,
position_ms: float | None = None,
*,
visible: bool = True,
z: int = 0,
fit_content: bool = False
)
Bases: Component
A native-resolution terminal image or real-time GIF component.
Pass an Image to a runtime render for a zero-delay still/first
frame. Animated formats play according to their source frame timings
when playing is True.
crop is intentionally the default fit: source pixels are never
resized. The source and viewport centers align, with oversized content
cropped and undersized content padded around the center.
Example
Image(source="logo.png", fit="contain")
Attributes:
-
source(ImageSource) –Encoded image path, bytes, stream, or decoded data.
-
fit(ImageFit) –Native crop, contain, cover, stretch, or adaptive placement.
-
loop(bool) –Whether an animation restarts after its final frame.
-
speed(float) –Playback-rate multiplier.
-
background(tuple[int, int, int]) –RGB color behind transparency and centered padding.
-
horizontal_pixels_per_cell(HorizontalPixelsPerCell) –One for lossless width or two for 2x2 source-pixel sampling.
-
correct_terminal_aspect(bool) –Whether to compensate 2x2 sampling for terminal cells that are approximately twice as tall as wide.
-
playing(bool) –Whether wall-clock time advances the animation.
-
position_ms(float | None) –Explicit playback position override in milliseconds.
Methods:
-
component_post_init–Validate configuration and decode the initial source.
-
play–Resume animation from the current playback position.
-
pause–Freeze animation at the current playback position.
-
restart–Restart animation timing at the next render.
-
seek–Move playback to a source timestamp without delaying a render.
-
get_frame_index–Return the source frame active at an elapsed playback time.
-
get_size–Return the native terminal cell dimensions of the source.
-
compose–Compose the current timed frame for the target terminal area.
-
get_frame–Optional frame/panel chrome around composed content.
-
before_render–Called before rendering; returns the effective render area.
-
after_render–Called after rendering for optional post-paint work.
-
compose_extra_small–Compose content when the viewport is extra small (< 40 cols).
-
compose_small–Compose content when the viewport is small (40–79 cols).
-
compose_medium–Compose content when the viewport is medium (80–119 cols).
-
compose_large–Compose content when the viewport is large (120–159 cols).
-
compose_extra_large–Compose content when the viewport is extra large (>= 160 cols).
-
handle_keyboard–Optional keyboard handler while focused.
-
handle_paste–Optional paste handler while focused.
source
class-attribute
instance-attribute
¶
source: ImageSource = ''
Encoded image path, bytes, stream, or decoded image data.
fit
class-attribute
instance-attribute
¶
fit: ImageFit = 'crop'
Native crop, contain, cover, stretch, or adaptive placement.
loop
class-attribute
instance-attribute
¶
loop: bool = True
Whether an animation restarts after its final frame.
background
class-attribute
instance-attribute
¶
RGB color behind transparency and centered padding.
horizontal_pixels_per_cell
class-attribute
instance-attribute
¶
horizontal_pixels_per_cell: HorizontalPixelsPerCell = 1
Adjacent source pixels sampled into each terminal cell.
correct_terminal_aspect
class-attribute
instance-attribute
¶
correct_terminal_aspect: bool = False
Whether 2x2 sampling preserves physical terminal proportions.
playing
class-attribute
instance-attribute
¶
playing: bool = True
Whether wall-clock time advances the animation.
position_ms
class-attribute
instance-attribute
¶
position_ms: float | None = None
Explicit playback position override in milliseconds.
fit_content
class-attribute
instance-attribute
¶
fit_content: bool = dataclasses.field(
default=False, kw_only=True
)
Whether layout should use the image's natural cell size.
visible
class-attribute
instance-attribute
¶
visible: bool = dataclasses.field(
default=True, kw_only=True
)
Whether this component paints at all.
z
class-attribute
instance-attribute
¶
z: int = dataclasses.field(default=0, kw_only=True)
Stacking order among sibling content.
component_post_init
¶
play
¶
Resume animation from the current playback position.
Source code in xnano/components/image.py
pause
¶
restart
¶
seek
¶
seek(position_ms: float) -> None
Move playback to a source timestamp without delaying a render.
Parameters:
-
position_ms(float) –Source animation timestamp in milliseconds.
Source code in xnano/components/image.py
get_frame_index
¶
Return the source frame active at an elapsed playback time.
Parameters:
-
elapsed_ms(float) –Playback time in milliseconds.
Returns:
-
int–The zero-based active frame index.
Source code in xnano/components/image.py
get_size
¶
get_size(ctx: ComponentRenderContext) -> Size
Return the native terminal cell dimensions of the source.
Source code in xnano/components/image.py
compose
¶
compose(ctx: ComponentRenderContext) -> CellCanvas
Compose the current timed frame for the target terminal area.
Source code in xnano/components/image.py
before_render
¶
before_render(
ctx: ComponentRenderContext[StateT], area: "Area"
) -> "Area"
after_render
¶
after_render(
ctx: ComponentRenderContext[StateT], area: "Area"
) -> None
compose_extra_small
¶
compose_extra_small(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is extra small (< 40 cols).
Optional responsive counterpart to :meth:compose. When
overridden, it is used instead of compose while the window
is in this size tier. Overriding any compose_* variant opts the
component into breakpoint dispatch; a component that overrides none
pays no per-frame cost.
Source code in xnano/components/component.py
compose_small
¶
compose_small(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is small (40–79 cols).
See :meth:compose_extra_small.
compose_medium
¶
compose_medium(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is medium (80–119 cols).
See :meth:compose_extra_small.
compose_large
¶
compose_large(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is large (120–159 cols).
See :meth:compose_extra_small.
compose_extra_large
¶
compose_extra_large(
ctx: ComponentRenderContext[StateT],
) -> Content | None
Compose content when the viewport is extra large (>= 160 cols).
See :meth:compose_extra_small.
Source code in xnano/components/component.py
handle_keyboard
¶
handle_keyboard(keyboard: 'KeyboardEventData') -> bool