### Install resvg-py from source using uv Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Install resvg-py directly from its GitHub repository using uv. This method fetches and builds the source code. ```bash uv pip install git+https://github.com/baseplate-admin/resvg-py.git ``` -------------------------------- ### Verify resvg-py installation Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Verify that resvg-py has been installed correctly by importing the library and checking its version attributes. ```python >>> import resvg_py >>> resvg_py.__version__ '0.3.2' >>> resvg_py.__resvg_version__ '0.47.0' ``` -------------------------------- ### Install resvg-py using uv Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Install the resvg-py library using the uv package manager. This is an alternative to pip for faster dependency resolution. ```bash uv pip install resvg_py ``` -------------------------------- ### Install resvg-py in a virtual environment with Poetry Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Install resvg-py within a virtual environment managed by Poetry without adding it to pyproject.toml. This is useful for temporary installations or testing. ```bash poetry run pip install resvg_py ``` -------------------------------- ### Install resvg-py from source using pip Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Install resvg-py directly from its GitHub repository using pip. This method fetches and builds the source code. ```bash pip install git+https://github.com/baseplate-admin/resvg-py.git ``` -------------------------------- ### Install resvg_py Source: https://github.com/baseplate-admin/resvg-py/blob/master/README.md Use pip to install the resvg_py package. Requires Python 3.10 or higher. ```python pip install resvg_py ``` -------------------------------- ### Install resvg-py using pip within a Conda environment Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Create a Conda environment and then install the latest resvg-py version using pip. This ensures you get the most recent release. ```bash conda create -n myenv python=3.12 conda activate myenv pip install resvg_py ``` -------------------------------- ### Install resvg-py using Pipx Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Install the resvg-py library using Pipx. Note: Pipx is intended for CLI applications, not libraries like resvg-py. ```bash pipx install resvg_py ``` -------------------------------- ### Install resvg-py from source using maturin Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Build and install resvg-py from its source code using maturin. This is necessary if no pre-compiled wheel is available for your platform. ```bash git clone https://github.com/baseplate-admin/resvg-py.git cd resvg-py maturin develop ``` -------------------------------- ### Install resvg-py using Pipenv Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Install the resvg-py library using the Pipenv package manager. This command adds the package to your Pipfile. ```bash pipenv install resvg_py ``` -------------------------------- ### Install resvg-py using pip Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Install the resvg-py library using the pip package manager. This is the most common method and does not require system libraries or a build step. ```bash python -m pip install resvg_py ``` -------------------------------- ### Install resvg-py using Conda Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Install the resvg-py library from the conda-forge channel. Note that Conda-forge packages may lag behind PyPI releases. ```bash conda install -c conda-forge resvg_py ``` -------------------------------- ### Sync Dependencies Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/contributing.md Use this command to synchronize project dependencies using uv. ```bash uv sync ``` -------------------------------- ### Run Test Suite Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/contributing.md Execute the project's test suite using pytest. ```bash pytest . ``` -------------------------------- ### Load Custom Font File Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/fonts.md Use the `font_files` argument to specify a path to a custom font file for rendering. ```python resvg_py.svg_to_bytes( svg_string=svg, font_files=["/path/to/MyFont.ttf"], ) ``` -------------------------------- ### Add resvg-py as a project dependency with Poetry Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Add resvg-py as a project dependency using Poetry. This command updates your pyproject.toml file. ```bash poetry add resvg_py ``` -------------------------------- ### Add resvg-py as a project dependency with uv Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Add resvg-py as a project dependency using uv. This command updates your project's dependency file. ```bash uv add resvg_py ``` -------------------------------- ### Background Color Parse Error Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/exceptions.md Raised when the 'background' parameter string cannot be parsed as a valid CSS color. ```python ValueError: Error background: ``` ```python >>> resvg_py.svg_to_bytes(svg_string=svg, background="not-a-color") ValueError: Error background: ... ``` -------------------------------- ### Add resvg-py as a project dependency with PDM Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/installation.md Add resvg-py as a project dependency using PDM. This command updates your pdm.lock and pyproject.toml files. ```bash pdm add resvg_py ``` -------------------------------- ### Load Custom Font Directory Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/fonts.md Use the `font_dirs` argument to specify a directory containing custom font files for rendering. ```python resvg_py.svg_to_bytes( svg_string=svg, font_dirs=["/path/to/fonts/"], ) ``` -------------------------------- ### Render SVG File to PNG Bytes Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/usage.md Convert an SVG file directly to PNG bytes by providing the file path. The library automatically handles `.svgz` compressed files. ```python png_bytes = resvg_py.svg_to_bytes(svg_path="/path/to/image.svg") ``` -------------------------------- ### Skip System Fonts Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/fonts.md Set `skip_system_fonts=True` to prevent the use of system fonts. Ensure custom fonts are provided via `font_files` or `font_dirs` to avoid rendering issues. ```python resvg_py.svg_to_bytes( svg_string=svg, skip_system_fonts=True, font_files=["/path/to/MyFont.ttf"], ) ``` -------------------------------- ### Resize SVG at Render Time Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/usage.md Control the output dimensions of the PNG by specifying width, height, or a zoom factor. If only one dimension is provided, the other scales proportionally. The `svg` variable is assumed to be defined. ```python # fixed width and height resvg_py.svg_to_bytes(svg_string=svg, width=800, height=600) # width only -- height scales proportionally resvg_py.svg_to_bytes(svg_string=svg, width=1920) # height only -- width scales proportionally resvg_py.svg_to_bytes(svg_string=svg, height=1080) # zoom factor resvg_py.svg_to_bytes(svg_string=svg, zoom=2) ``` -------------------------------- ### Render SVG String to PNG Bytes Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/usage.md Use this snippet to convert an SVG markup string into raw PNG bytes. Ensure the `resvg_py` library is imported. ```python import resvg_py svg_string = """ """ png_bytes: bytes = resvg_py.svg_to_bytes(svg_string=svg_string) ``` -------------------------------- ### Set Background Color for PNG Render Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/usage.md Specify a background color for the rendered PNG using hexadecimal or RGBA color formats. The `svg` variable is assumed to be defined. ```python resvg_py.svg_to_bytes(svg_string=svg, background="#f0f0f0") resvg_py.svg_to_bytes(svg_string=svg, background="rgba(255,0,0,0.5)") ``` -------------------------------- ### Enable Debug Logging in resvg-py Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/debugging.md Pass `log_information=True` to `svg_to_bytes()` to print resvg's internal warnings and errors to stdout. The logger is initialized once per process; the flag only needs to be `True` on the first call. ```python import resvg_py png_bytes = resvg_py.svg_to_bytes( svg_string=svg, log_information=True, ) ``` -------------------------------- ### Encode PNG Bytes to Base64 Data URI Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/usage.md After rendering SVG to PNG bytes, encode them into a base64 string to create a data URI. This requires the `base64` module. ```python import base64 png_bytes = resvg_py.svg_to_bytes(svg_string=svg) b64 = base64.b64encode(png_bytes).decode("utf-8") print(f"data:image/png;base64,{b64}") ``` -------------------------------- ### Convert SVG to PNG Bytes Source: https://github.com/baseplate-admin/resvg-py/blob/master/README.md Import the resvg_py library and use the svg_to_bytes function to convert an SVG string into PNG image bytes. Ensure the SVG string is valid. ```python import resvg_py svg_string = """   " print(resvg_py.svg_to_bytes(svg_string=svg_string)) ``` -------------------------------- ### Convert SVG to PNG Bytes Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/index.md Use this snippet to convert an SVG string into PNG image bytes. It allows specifying the output dimensions. The resulting bytes can then be written to a file. ```python import resvg_py png_bytes = resvg_py.svg_to_bytes( svg_string='', width=200, height=200, ) with open("output.png", "wb") as f: f.write(png_bytes) ``` -------------------------------- ### svg_to_bytes Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/api.md Converts SVG markup or a file path into PNG bytes. It offers extensive customization options for rendering, including dimensions, fonts, and rendering policies. ```APIDOC ## svg_to_bytes ### Description Converts SVG markup or a file path into PNG bytes. It offers extensive customization options for rendering, including dimensions, fonts, and rendering policies. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters Reference | Parameter | Type | Default | Description | |---|---|---|---| | `svg_string` | `str | None` | `None` | SVG markup as a UTF-8 string. Preferred over `svg_path`. | | `svg_path` | `str | None` | `None` | Path to an SVG file. `.svgz` auto-decompresses. | | `background` | `str | None` | `None` | CSS color for the canvas background. | | `skip_system_fonts` | `bool` | `False` | Skip loading system fonts. Provide `font_files` or `font_dirs`. | | `log_information` | `bool` | `False` | Enable resvg debug logging to stdout. | | `width` | `int | None` | `None` | Target pixel width. | | `height` | `int | None` | `None` | Target pixel height. | | `zoom` | `int | None` | `None` | Integer zoom multiplier. | | `dpi` | `int` | `0` | DPI override (`0` = SVG default). | | `style_sheet` | `str | None` | `None` | CSS stylesheet string applied during parse. | | `resources_dir` | `str | None` | `None` | Directory for `xlink:href` resource references. | | `languages` | `list[str]` | `[]` | Preferred languages for `` elements. | | `font_size` | `int` | `16` | Default font size in pixels. | | `font_family` | `str | None` | OS-dependent | Default (generic) font family. | | `serif_family` | `str | None` | OS-dependent | Serif generic family override. | | `sans_serif_family` | `str | None` | OS-dependent | Sans-serif generic family override. | | `cursive_family` | `str | None` | OS-dependent | Cursive generic family override. | | `fantasy_family` | `str | None` | OS-dependent | Fantasy generic family override. | | `monospace_family` | `str | None` | OS-dependent | Monospace generic family override. | | `font_files` | `list[str] | None` | `None` | Explicit font file paths to load. | | `font_dirs` | `list[str] | None` | `None` | Directories recursively scanned for fonts. | | `shape_rendering` | `str` | `"geometric_precision"` | Shape rendering policy. | | `text_rendering` | `str` | `"optimize_legibility"` | Text rendering policy. | | `image_rendering` | `str` | `"optimize_quality"` | Image rendering policy. | ### Request Example None ### Response #### Success Response Returns PNG image bytes. #### Response Example None ``` -------------------------------- ### Invalid Rendering Option Value Error Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/exceptions.md Raised when a rendering parameter is set to a value not accepted by the library. Check the 'Accepted values' table for correct options. ```python ValueError: The value of 'shape_rendering' must be one of ... ``` ```python >>> resvg_py.svg_to_bytes(svg_string=svg, shape_rendering="best") ValueError: The value of 'shape_rendering' must be one of 'optimize_speed','crisp_edges','geometric_precision'. It is currently 'best' ``` -------------------------------- ### Invalid SVG Input Error Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/exceptions.md Raised when the provided SVG input is empty, invalid, or a compressed SVG (.svgz) fails to decompress. ```python ValueError: 'svg_string' is empty or 'svg_path' contains empty invalid svg ``` -------------------------------- ### Override Default Font Families Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/fonts.md Override specific generic font families like `font_family` or `monospace_family` with custom font names. ```python resvg_py.svg_to_bytes( svg_string=svg, font_family="Inter", monospace_family="Fira Code", ) ``` -------------------------------- ### General Rendering/Processing Error Source: https://github.com/baseplate-admin/resvg-py/blob/master/docs/exceptions.md Raised for various failures during SVG processing, including XML parsing, pixmap creation, or PNG encoding. The message will contain the underlying Rust error. ```python ValueError: ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.