Can I specify scaling when using the cairosvg module inside of python
Asked Answered
S

3

5

The command line version of cairosvg allows scaling. Here is the output of the help function:

cairosvg -h
usage: cairosvg [-h] [-v] [-f {pdf,png,ps,svg}] [-d DPI] [-W WIDTH]
            [-H HEIGHT] [-s SCALE] [-u] [-o OUTPUT]
            input

CairoSVG - A simple SVG converter based on Cairo.

positional arguments:
   input                 input filename or URL

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program\'s version number and exit
  -f {pdf,png,ps,svg}, --format {pdf,png,ps,svg}
                        output format
  -d DPI, --dpi DPI     ratio between 1 inch and 1 pixel
  -W WIDTH, --width WIDTH
                        width of the parent container in pixels
  -H HEIGHT, --height HEIGHT
                        height of the parent container in pixels
  -s SCALE, --scale SCALE
                        output scaling factor

see also cairosvg documentation

How can one specify a scaling factor when using cairosvg2png inside of a Python script?

Stagnant answered 20/3, 2017 at 4:17 Comment(1)
this is clever #4256607 but I am hoping for a native python solution.Stagnant
I
4

On this commit 2 new parameters where added.

[... other params]
output_width=None,
output_height=None

For me output_height and output_width did the job.

Ironsides answered 10/7, 2020 at 9:2 Comment(0)
F
5

I had the same question - the documentation doesn't make this explicit, but as it turns out, it's as simple as passing the scale=2.0 keyword argument to the svg2png method.

import cairosvg

input_fn = '/home/me/input.svg'
output_fn = '/home/me/output.png'

cairosvg.svg2png(url=input_fn, write_to=output_fn, scale=2.0)

This definitely works for me on version 2.0.3.

Forgat answered 9/9, 2017 at 21:15 Comment(0)
I
4

On this commit 2 new parameters where added.

[... other params]
output_width=None,
output_height=None

For me output_height and output_width did the job.

Ironsides answered 10/7, 2020 at 9:2 Comment(0)
S
0

the documentation has been updated at: http://cairosvg.org/documentation/ with API documentation commit

Stagnant answered 23/3, 2017 at 4:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.