pixelplotlib 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ """pixelplotlib: scientific plots with 8-bit and 16-bit pixel-art graphics.
2
+
3
+ Quick start::
4
+
5
+ import numpy as np
6
+ import pixelplotlib as ppl
7
+
8
+ x = np.linspace(0, 10, 200)
9
+ fig, ax = ppl.subplots(style="16bit")
10
+ ax.plot(x, np.sin(x), label="sin")
11
+ ax.set(title="HELLO WAVES", xlabel="t", ylabel="A")
12
+ ax.legend()
13
+ fig.save("waves.png")
14
+ """
15
+
16
+ from importlib.metadata import PackageNotFoundError, version
17
+
18
+ from . import colormaps, colors, palettes, sixel, styles
19
+ from .animation import animate, save_gif
20
+ from .axes import Axes
21
+ from .canvas import Canvas
22
+ from .colormaps import Colormap, get_cmap, register_cmap
23
+ from .dither import pixelate
24
+ from .figure import Figure
25
+ from .fonts import FONT_3X5, FONT_5X7, BitmapFont
26
+ from .markers import MARKERS
27
+ from .pyplot import * # noqa: F401,F403
28
+ from .pyplot import __all__ as _pyplot_all
29
+ from .styles import Style, available, context, get_style, register, use
30
+
31
+ try:
32
+ __version__ = version("pixelplotlib")
33
+ except PackageNotFoundError: # running from a source checkout without installation
34
+ __version__ = "0.0.0"
35
+
36
+ __all__ = [
37
+ "Axes", "BitmapFont", "Canvas", "Colormap", "Figure", "Style", "FONT_3X5", "FONT_5X7",
38
+ "MARKERS", "animate", "available", "colormaps", "colors", "context", "get_cmap",
39
+ "get_style", "palettes", "pixelate", "register", "register_cmap", "save_gif", "sixel",
40
+ "styles", "use", *_pyplot_all,
41
+ ]
@@ -0,0 +1,73 @@
1
+ """Animated GIF export."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ from collections.abc import Callable, Iterable
7
+
8
+ import numpy as np
9
+
10
+ from .figure import Figure, apply_scanlines
11
+
12
+ FrameLike = Figure | np.ndarray
13
+
14
+
15
+ def _frame_array(frame: FrameLike, scale: int) -> np.ndarray:
16
+ if isinstance(frame, Figure):
17
+ return frame.render().to_array(scale)
18
+ arr = np.asarray(frame, dtype=np.uint8)
19
+ if scale > 1:
20
+ arr = np.repeat(np.repeat(arr, scale, axis=0), scale, axis=1)
21
+ return arr
22
+
23
+
24
+ def save_gif(frames: Iterable[FrameLike], path: str | os.PathLike, fps: float = 10,
25
+ scale: int | None = None, loop: int = 0, scanlines: bool = False) -> None:
26
+ """Write frames (figures or RGB arrays) to an animated GIF.
27
+
28
+ Pixel-art frames use few colors, so a shared exact palette is built whenever
29
+ possible (no lossy quantization).
30
+ """
31
+ from PIL import Image
32
+
33
+ frames = list(frames)
34
+ if not frames:
35
+ raise ValueError("no frames to save")
36
+ if scale is None:
37
+ scale = frames[0].scale if isinstance(frames[0], Figure) else 1
38
+ arrays = [_frame_array(f, scale) for f in frames]
39
+ if scanlines and scale >= 2:
40
+ arrays = [apply_scanlines(a, scale) for a in arrays]
41
+ packed = [(a[..., 0].astype(np.uint32) << 16) | (a[..., 1].astype(np.uint32) << 8)
42
+ | a[..., 2] for a in arrays]
43
+ colors = np.unique(np.concatenate([p.ravel() for p in packed]))
44
+ images = []
45
+ if len(colors) <= 256:
46
+ pal = np.zeros((256, 3), dtype=np.uint8)
47
+ pal[: len(colors), 0] = colors >> 16
48
+ pal[: len(colors), 1] = (colors >> 8) & 0xFF
49
+ pal[: len(colors), 2] = colors & 0xFF
50
+ flat_pal = pal.ravel().tolist()
51
+ for p in packed:
52
+ idx = np.searchsorted(colors, p).astype(np.uint8)
53
+ im = Image.fromarray(idx, "P")
54
+ im.putpalette(flat_pal)
55
+ images.append(im)
56
+ else:
57
+ images = [Image.fromarray(a, "RGB").quantize(256, dither=Image.Dither.NONE)
58
+ for a in arrays]
59
+ duration = int(round(1000 / fps))
60
+ images[0].save(path, save_all=True, append_images=images[1:], duration=duration,
61
+ loop=loop, optimize=False, disposal=1)
62
+
63
+
64
+ def animate(func: Callable[[int], FrameLike], frames: int | Iterable, path: str | os.PathLike,
65
+ fps: float = 10, scale: int | None = None, loop: int = 0,
66
+ scanlines: bool = False) -> None:
67
+ """Call ``func(frame)`` for every frame and save the results as a GIF.
68
+
69
+ ``func`` must return a :class:`Figure` (or an RGB array).
70
+ """
71
+ seq = range(frames) if isinstance(frames, int) else frames
72
+ save_gif((func(i) for i in seq), path, fps=fps, scale=scale, loop=loop,
73
+ scanlines=scanlines)