pixel-map 0.2.3__tar.gz → 0.3.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pixel-map
3
- Version: 0.2.3
3
+ Version: 0.3.0
4
4
  Summary: A Python CLI tool for plotting geo files in the terminal
5
5
  Author-Email: Kamil Raczycki <kraczycki@kraina.ai>
6
6
  License: MIT
@@ -10,26 +10,25 @@ Classifier: Topic :: Scientific/Engineering :: GIS
10
10
  Classifier: Intended Audience :: Science/Research
11
11
  Classifier: Intended Audience :: Developers
12
12
  Classifier: Programming Language :: Python :: 3
13
- Classifier: Programming Language :: Python :: 3.9
14
13
  Classifier: Programming Language :: Python :: 3.10
15
14
  Classifier: Programming Language :: Python :: 3.11
16
15
  Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
17
  Classifier: Operating System :: Unix
18
18
  Classifier: Operating System :: MacOS
19
19
  Classifier: Operating System :: Microsoft :: Windows
20
20
  Project-URL: Homepage, https://github.com/RaczeQ/pixel-map
21
21
  Project-URL: Repository, https://github.com/RaczeQ/pixel-map
22
22
  Project-URL: Changelog, https://github.com/RaczeQ/pixel-map/blob/main/CHANGELOG.md
23
- Requires-Python: >=3.9
23
+ Requires-Python: >=3.10
24
24
  Requires-Dist: rich>=12.0.0
25
25
  Requires-Dist: typer[all]>=0.9.0
26
- Requires-Dist: img2unicode[n2]>=0.1a11; sys_platform != "win32" and sys_platform != "darwin"
27
- Requires-Dist: img2unicode>=0.1a11; sys_platform == "win32" or sys_platform == "darwin"
28
26
  Requires-Dist: geopandas>=0.8
29
27
  Requires-Dist: numpy>=1.26.0
30
28
  Requires-Dist: contextily>=1
31
29
  Requires-Dist: matplotlib>=3.2.0
32
30
  Requires-Dist: pyarrow>=16.0.0
31
+ Requires-Dist: pillow>=10.1.0
33
32
  Description-Content-Type: text/markdown
34
33
 
35
34
  # pixel-map
@@ -196,7 +195,7 @@ $ pixel-map --example london_buildings --no-bg -r ascii-bw
196
195
 
197
196
  - `numpy (>=1.26.0)`: For transforming matplotlib array into an image
198
197
 
199
- - `img2unicode[n2] (>=0.1a11)`: For transforming generated plot image into a list of unicode characters for the terminal
198
+ - `pillow (>=10.1.0)`: For rasterising ASCII glyph templates and downscaling the canvas
200
199
 
201
200
  ## How does it work?
202
201
 
@@ -206,7 +205,10 @@ $ pixel-map --example london_buildings --no-bg -r ascii-bw
206
205
  4. Plot bounding box is expanded to the canvas ratio (based on terminal size).
207
206
  5. Basemap is loaded with `contextily`.
208
207
  6. Canvas is copied to the image as an `NumPy` array.
209
- 7. `img2unicode` is used to render image using unicode characters with color.
208
+ 7. Image is downsampled and converted to a grid of Unicode characters with
209
+ foreground/background colours using the native dual-colour engine in
210
+ `pixel_map/terminal_renderers.py` (Lab perceptual distance, BLAS-accelerated
211
+ character selection).
210
212
  8. `Rich` string output is generated using the list of unicode characters with background and foreground colors.
211
213
  9. Output is printed to the console.
212
214
 
@@ -162,7 +162,7 @@ $ pixel-map --example london_buildings --no-bg -r ascii-bw
162
162
 
163
163
  - `numpy (>=1.26.0)`: For transforming matplotlib array into an image
164
164
 
165
- - `img2unicode[n2] (>=0.1a11)`: For transforming generated plot image into a list of unicode characters for the terminal
165
+ - `pillow (>=10.1.0)`: For rasterising ASCII glyph templates and downscaling the canvas
166
166
 
167
167
  ## How does it work?
168
168
 
@@ -172,7 +172,10 @@ $ pixel-map --example london_buildings --no-bg -r ascii-bw
172
172
  4. Plot bounding box is expanded to the canvas ratio (based on terminal size).
173
173
  5. Basemap is loaded with `contextily`.
174
174
  6. Canvas is copied to the image as an `NumPy` array.
175
- 7. `img2unicode` is used to render image using unicode characters with color.
175
+ 7. Image is downsampled and converted to a grid of Unicode characters with
176
+ foreground/background colours using the native dual-colour engine in
177
+ `pixel_map/terminal_renderers.py` (Lab perceptual distance, BLAS-accelerated
178
+ character selection).
176
179
  8. `Rich` string output is generated using the list of unicode characters with background and foreground colors.
177
180
  9. Output is printed to the console.
178
181
 
@@ -1,4 +1,4 @@
1
1
  """Pixel-map library."""
2
2
 
3
3
  __app_name__ = "pixel-map"
4
- __version__ = "0.2.3"
4
+ __version__ = "0.3.0"
@@ -1,7 +1,7 @@
1
1
  """Main CLI module."""
2
2
 
3
3
  from contextlib import suppress
4
- from typing import Annotated, Optional, cast
4
+ from typing import Annotated, cast
5
5
 
6
6
  import click
7
7
  import typer
@@ -89,7 +89,7 @@ def plot(
89
89
  ),
90
90
  ],
91
91
  bbox: Annotated[
92
- Optional[str],
92
+ str | None,
93
93
  typer.Option(
94
94
  "--bbox",
95
95
  "-b",
@@ -126,7 +126,7 @@ def plot(
126
126
  ),
127
127
  ] = True,
128
128
  colors: Annotated[
129
- Optional[str],
129
+ str | None,
130
130
  typer.Option(
131
131
  "--color",
132
132
  "-c",
@@ -136,7 +136,7 @@ def plot(
136
136
  ),
137
137
  ] = None,
138
138
  alphas: Annotated[
139
- Optional[str],
139
+ str | None,
140
140
  typer.Option(
141
141
  "--alpha",
142
142
  "--opacity",
@@ -147,7 +147,7 @@ def plot(
147
147
  ),
148
148
  ] = None,
149
149
  basemap_provider: Annotated[
150
- Optional[str],
150
+ str | None,
151
151
  typer.Option(
152
152
  "--basemap",
153
153
  "--tileset",
@@ -155,8 +155,8 @@ def plot(
155
155
  metavar="TILES",
156
156
  help=(
157
157
  "Set the basemap provider. Can be any value parsed by xyzservices library."
158
- " Defaults to [bold dark_orange]CartoDB.DarkMatterNoLabels[/bold dark_orange]"
159
- " if --dark or [bold dark_orange]CartoDB.PositronNoLabels[/bold dark_orange]"
158
+ " Defaults to [bold dark_orange]ESRI World_Dark_Gray_Base[/bold dark_orange]"
159
+ " if --dark or [bold dark_orange]ESRI World_Light_Gray_Base[/bold dark_orange]"
160
160
  " if --light."
161
161
  ),
162
162
  show_default=False,
@@ -182,7 +182,7 @@ def plot(
182
182
  ),
183
183
  ] = False,
184
184
  background_color: Annotated[
185
- Optional[str],
185
+ str | None,
186
186
  typer.Option(
187
187
  "--background-color",
188
188
  "--bg-color",
@@ -192,7 +192,7 @@ def plot(
192
192
  ),
193
193
  ] = None,
194
194
  console_width: Annotated[
195
- Optional[int],
195
+ int | None,
196
196
  typer.Option(
197
197
  "--width",
198
198
  metavar="INT",
@@ -201,7 +201,7 @@ def plot(
201
201
  ),
202
202
  ] = None,
203
203
  console_height: Annotated[
204
- Optional[int],
204
+ int | None,
205
205
  typer.Option(
206
206
  "--height",
207
207
  metavar="INT",
@@ -214,10 +214,12 @@ def plot(
214
214
  typer.Option(
215
215
  "--dpi",
216
216
  metavar="INT",
217
- help=("DPI used to get better quality matplotlib plot before rendering to terminal."),
217
+ help=("DPI used to rasterise the matplotlib plot. Setting this to 8"
218
+ " (matching the renderer's subpixel width) skips the PIL resize"
219
+ " for a faster path; higher values improve anti-aliasing quality."),
218
220
  show_default=True,
219
221
  ),
220
- ] = 10,
222
+ ] = 8,
221
223
  example_files: Annotated[
222
224
  bool,
223
225
  typer.Option(
@@ -231,7 +233,7 @@ def plot(
231
233
  ),
232
234
  ] = False,
233
235
  version: Annotated[
234
- Optional[bool],
236
+ bool | None,
235
237
  typer.Option(
236
238
  "--version",
237
239
  "-v",
@@ -285,11 +287,11 @@ def plot(
285
287
  if no_background:
286
288
  basemap_provider = None
287
289
 
288
- parsed_colors: Optional[list[str]] = colors # type: ignore[assignment]
290
+ parsed_colors: list[str] | None = colors # type: ignore[assignment]
289
291
  if not parsed_colors:
290
292
  parsed_colors = [color]
291
293
 
292
- parsed_alphas: Optional[list[float]] = alphas # type: ignore[assignment]
294
+ parsed_alphas: list[float] | None = alphas # type: ignore[assignment]
293
295
  if not parsed_alphas:
294
296
  parsed_alphas = [1]
295
297
 
@@ -308,7 +310,7 @@ def plot(
308
310
  plot_geo_data(
309
311
  files,
310
312
  renderer=renderer,
311
- bbox=cast(Optional[tuple[float, float, float, float]], bbox),
313
+ bbox=cast("tuple[float, float, float, float] | None", bbox),
312
314
  color=parsed_colors,
313
315
  alpha=parsed_alphas,
314
316
  basemap_provider=basemap_provider,
@@ -7,7 +7,7 @@ unicode characters.
7
7
 
8
8
  import os
9
9
  from pathlib import Path
10
- from typing import Any, Optional, Union
10
+ from typing import Any
11
11
 
12
12
  import contextily as cx
13
13
  import geopandas as gpd
@@ -35,15 +35,15 @@ EPSG_3857_BOUNDS = (-20037508.34, -20048966.1, 20037508.34, 20048966.1)
35
35
  def plot_geo_data(
36
36
  files: list[str],
37
37
  renderer: str,
38
- bbox: Optional[tuple[float, float, float, float]] = None,
39
- color: Union[str, list[str]] = "C0",
40
- alpha: Union[float, list[float]] = 1.0,
41
- basemap_provider: Optional[str] = None,
42
- background_color: Optional[str] = None,
38
+ bbox: tuple[float, float, float, float] | None = None,
39
+ color: str | list[str] = "C0",
40
+ alpha: float | list[float] = 1.0,
41
+ basemap_provider: str | None = None,
42
+ background_color: str | None = None,
43
43
  no_border: bool = False,
44
- console_width: Optional[int] = None,
45
- console_height: Optional[int] = None,
46
- plotting_dpi: int = 10,
44
+ console_width: int | None = None,
45
+ console_height: int | None = None,
46
+ plotting_dpi: int = 8,
47
47
  ) -> None:
48
48
  """
49
49
  Plot the geo data into a terminal.
@@ -156,8 +156,20 @@ def plot_geo_data(
156
156
  ax.set_position((0, 0, 1, 1))
157
157
  canvas.draw()
158
158
 
159
- image_flat = np.frombuffer(canvas.tostring_rgb(), dtype="uint8") # (H * W * 3,)
160
- image = image_flat.reshape(*reversed(canvas.get_width_height()), 3)
159
+ # buffer_rgba() replaces the deprecated (and removed in matplotlib 3.10)
160
+ # tostring_rgb() method. It returns RGBA data; we keep only RGB.
161
+ # On high-DPI displays (e.g. macOS Retina), get_width_height(physical=True)
162
+ # returns the physical pixel dimensions matching the buffer. The fallback
163
+ # below handles older backends that lack the physical= parameter.
164
+ image_flat = np.frombuffer(canvas.buffer_rgba(), dtype="uint8") # (H * W * 4,)
165
+ canvas_w, canvas_h = canvas.get_width_height(physical=True)
166
+ total_pixels = len(image_flat) // 4
167
+ if canvas_w * canvas_h != total_pixels:
168
+ # Derive actual physical dimensions from the pixel count and the
169
+ # logical aspect ratio, avoiding lossy rounding of the scale factor.
170
+ canvas_h = round((total_pixels * canvas_h / canvas_w) ** 0.5)
171
+ canvas_w = total_pixels // canvas_h
172
+ image = image_flat.reshape(canvas_h, canvas_w, 4)[:, :, :3]
161
173
 
162
174
  with _get_progress_object(console) as progress:
163
175
  progress.add_task("Rendering geo data", total=None)
@@ -193,18 +205,23 @@ def plot_geo_data(
193
205
  )
194
206
 
195
207
 
208
+ ESRI_URL = "https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/{variant}/MapServer/tile/"
209
+
210
+
196
211
  def get_predefined_dark_style() -> tuple[str, str]:
197
212
  """Get default background and color for dark style."""
198
- return "CartoDB.DarkMatterNoLabels", "C1"
213
+ url = ESRI_URL.format(variant="World_Dark_Gray_Base") + "{z}/{y}/{x}"
214
+ return url, "C1"
199
215
 
200
216
 
201
217
  def get_predefined_light_style() -> tuple[str, str]:
202
218
  """Get default background and color for light style."""
203
- return "CartoDB.PositronNoLabels", "C0"
219
+ url = ESRI_URL.format(variant="World_Light_Gray_Base") + "{z}/{y}/{x}"
220
+ return url, "C0"
204
221
 
205
222
 
206
223
  def _load_geo_data(
207
- files: list[str], bbox: Optional[tuple[float, float, float, float]] = None
224
+ files: list[str], bbox: tuple[float, float, float, float] | None = None
208
225
  ) -> list[gpd.GeoSeries]:
209
226
  paths = [Path(file_path) for file_path in files]
210
227
  return [
@@ -218,7 +235,7 @@ def _load_geo_data(
218
235
 
219
236
 
220
237
  def _read_geoparquet_file(
221
- path: Path, bbox: Optional[tuple[float, float, float, float]] = None
238
+ path: Path, bbox: tuple[float, float, float, float] | None = None
222
239
  ) -> gpd.GeoDataFrame:
223
240
  try:
224
241
  return gpd.read_parquet(path, bbox=bbox)
@@ -248,6 +265,11 @@ def _expand_bbox_to_match_ratio(
248
265
  bottom = bottom - height_padding
249
266
  top = top + height_padding
250
267
 
268
+ left = max(left, EPSG_3857_BOUNDS[0])
269
+ bottom = max(bottom, EPSG_3857_BOUNDS[1])
270
+ right = min(right, EPSG_3857_BOUNDS[2])
271
+ top = min(top, EPSG_3857_BOUNDS[3])
272
+
251
273
  new_minx, new_miny = TRANSFORMER.transform(left, bottom, direction=TransformDirection.INVERSE)
252
274
  new_maxx, new_maxy = TRANSFORMER.transform(right, top, direction=TransformDirection.INVERSE)
253
275
 
@@ -0,0 +1,15 @@
1
+ """
2
+ Terminal renderers.
3
+
4
+ Re-exports the native renderers implemented in :mod:`pixel_map.terminal_renderers` which replace the
5
+ previous ``img2unicode`` back-end. The public names and constructor signature ``(terminal_width,
6
+ terminal_height)`` are unchanged so that :mod:`pixel_map.plotter` and :mod:`pixel_map.__main__` keep
7
+ working without modification.
8
+ """
9
+
10
+ from pixel_map.terminal_renderers import ( # noqa: F401
11
+ AVAILABLE_RENDERERS as AVAILABLE_RENDERERS,
12
+ )
13
+ from pixel_map.terminal_renderers import (
14
+ TerminalRenderer as TerminalRenderer,
15
+ )