Get use of CairoSVG for rasterization.

This commit is contained in:
Sergey Vartanov 2021-08-21 12:48:12 +03:00
parent 7c43448275
commit 0cf935e599
4 changed files with 15 additions and 44 deletions

View file

@ -1,9 +1,11 @@
CairoSVG~=2.5.0
colour~=0.1.5 colour~=0.1.5
logging~=0.4.9.6
numpy>=1.18.1 numpy>=1.18.1
portolan~=1.0.1 portolan~=1.0.1
pycairo pycairo
pytest pytest~=6.2.2
pyyaml>=4.2b1 PyYAML>=4.2b1
shapely Shapely
svgwrite~=1.4 svgwrite~=1.4
urllib3>=1.25.6 urllib3>=1.25.6

View file

@ -1,35 +0,0 @@
"""
Rasterize vector graphics using Inkscape.
"""
import logging
import os
import subprocess
from pathlib import Path
__author__ = "Sergey Vartanov"
__email__ = "me@enzet.ru"
INKSCAPE_BIN: str = "INKSCAPE_BIN"
def rasterize(from_: Path, to_: Path, area: str = "", dpi: float = 90) -> None:
"""
Make PNG image out of SVG using Inkscape.
See https://inkscape.org/
"""
if "INKSCAPE_BIN" not in os.environ:
logging.fatal(
f"Environment variable {INKSCAPE_BIN} not set. Please, install "
f"Inkscape and set the variable to be able to rasterize SVG files."
)
commands: list[str] = [os.environ[INKSCAPE_BIN]]
commands += ["--export-png", to_.absolute()]
commands += ["--export-dpi", str(dpi)]
if area:
commands += ["--export-area", area]
commands += [from_.absolute()]
logging.info(f"Rasterize SVG file to {to_}...")
subprocess.run(commands)

View file

@ -6,7 +6,8 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
from roentgen.raster import rasterize import cairosvg
from roentgen.tile import Tile from roentgen.tile import Tile
from roentgen.workspace import workspace from roentgen.workspace import workspace
@ -42,7 +43,8 @@ class Handler(BaseHTTPRequestHandler):
if not svg_path.exists(): if not svg_path.exists():
tile = Tile(x, y, zoom) tile = Tile(x, y, zoom)
tile.draw(tile_path, self.cache) tile.draw(tile_path, self.cache)
rasterize(svg_path, png_path) cairosvg.svg2png(file_obj=svg_path, write_to=str(png_path))
logging.info(f"SVG file is rasterized to {png_path}.")
if zoom != 18: if zoom != 18:
return return
if png_path.exists(): if png_path.exists():

View file

@ -9,6 +9,7 @@ from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
import cairosvg
import numpy as np import numpy as np
import svgwrite import svgwrite
@ -18,7 +19,6 @@ from roentgen.icon import ShapeExtractor
from roentgen.mapper import Map from roentgen.mapper import Map
from roentgen.osm_getter import NetworkError, get_osm from roentgen.osm_getter import NetworkError, get_osm
from roentgen.osm_reader import OSMData, OSMReader from roentgen.osm_reader import OSMData, OSMReader
from roentgen.raster import rasterize
from roentgen.scheme import Scheme from roentgen.scheme import Scheme
from roentgen.ui import BoundaryBox from roentgen.ui import BoundaryBox
from roentgen.util import MinMax from roentgen.util import MinMax
@ -84,7 +84,8 @@ class Tiles:
output_path: Path = file_path.with_suffix(".png") output_path: Path = file_path.with_suffix(".png")
if not output_path.exists(): if not output_path.exists():
rasterize(file_path, output_path) cairosvg.svg2png(file_obj=file_path, write_to=str(output_path))
logging.info(f"SVG file is rasterized to {output_path}.")
else: else:
logging.info(f"File {output_path} already exists.") logging.info(f"File {output_path} already exists.")
@ -135,7 +136,8 @@ class Tiles:
png_path: Path = cache_path / f"{self.boundary_box.get_format()}.png" png_path: Path = cache_path / f"{self.boundary_box.get_format()}.png"
if not png_path.exists(): if not png_path.exists():
rasterize(output_path, png_path) cairosvg.svg2png(file_obj=output_path, write_to=str(png_path))
logging.info(f"SVG file is rasterized to {png_path}.")
else: else:
logging.info(f"File {png_path} already exists.") logging.info(f"File {png_path} already exists.")