4

I have some images that I made in inkscape with .svg extension. These images are various size (256*265, 512*512, etc) in pixels.

How can I change the size of all these images at once in Linux?

2

2 Answers 2

4

I found the solution to my problem, the way to do this is by using rsvg-converter

and after searching in 3W i found this script here

all you need is make this script executable and run it followed by new size for your .svg file(s)

1
  • 3
    What if I want a scale factor instead of making all SVGs the same size? Commented Sep 6, 2020 at 6:09
3

Don't do it

Without knowing your reasoning for wanting to do this, my initial reaction is don't do it.

The size of an SVG file is really a suggestion—they are designed to be scaled up and down in size (hence scalable vector graphics). The height and width of an SVG file for most practical uses does not matter. If you want the image to be bigger on (for example) a web page, you can just set the height and width attributes. If you are exporting to a PNG file, you can set the dimensions to anything you want. The height and width attributes are, in fact, not even required to be there. If height or width isn't specified on the root <svg> element, it'll just default to 100%.

Fiddling around with height and width really misses one of the great benefits of SVG files, which is scalability.

But if you really want to do it...

There may be instances where you really do need to set the height and width of an SVG file, but this will usually be because of a limitation in your workflow or tools in my opinion.

SVG files are just XML files—you can open one up in a text editor and take a look. You can manipulate them just like you would any other XML file. Mozilla Dev Network has a fantastic reference for learning about the XML side of SVG files.

To manipulate an XML file, the traditional method is to use XSL, a stylesheet language for transforming one XML file into something else (usually another XML file).

Example

Create a text file named scale-svg.xsl and insert the following content:

<xsl:stylesheet version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="svg:svg">
        <xsl:copy>
            <xsl:copy-of select="svg:metadata|svg:defs|sodipodi:namedview"/>
            <xsl:element name="svg:g">
                <xsl:attribute name="transform">
                    <xsl:value-of select="concat(concat('scale(',$scale),')')"/>
                </xsl:attribute>
                <xsl:copy-of select="*[not(svg:metadata|svg:defs|sodipodi:namedview)]"/>
            </xsl:element>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Now you need to apply the stylesheet to an SVG file (or files). There are lots of XSLT programs available. If you are using Linux, xlstproc is probably already installed.

# This command will scale small.svg up by 3x
$ xsltproc --stringparam scale 3 scale-svg.xsl small.svg>big.svg

# This command will scale all SVG files in the current directory
$ for f in *.svg; do xsltproc --stringparam scale 3 scale-svg.xsl $f>3x_$f; done 

If you want to do something different (such as manipulate the height and width of the SVG document rather than scale it), you'll need to modify the XSL file to meet your needs. There are a lot of tutorials out there about XSL, such as this one at W3Schools.

There are other approaches you take take as well. A quick google searh also found svg-resizer, which is a JavaScript-based approach using rsvg. You can also probably get ImageMagick to do this too.

Can Inkscape do this for me?

While Inkscape does have batch processing abilities, I don't believe it can do this (again, the dimensions of an SVG file are suggestions, so the usefulness is limited).

Inkscape does have the ability to automate exporting SVG files at different sizes. The Inkscape command line options have the ability to manually set the height and width when exporting a PNG file.

5
  • compilation error: file scale-svg.xsl line 1 element xsl:stylesheet xsltParseStylesheetProcess : document is not a stylesheet Commented May 4, 2017 at 16:25
  • 2
    There are situations when some command line utilities (spritezero, for example) convert svg to raster images using viewBox, width and height attributes of svg, so these attributes (and path coordinates) are not always just suggestions.
    – unibasil
    Commented Feb 27, 2018 at 2:14
  • scale-svg.xsl:1: namespace error : Namespace prefix xsl on stylesheet is not defined Commented Sep 6, 2020 at 6:09
  • 1
    "Don't do it" is too broad, for some use cases. If Bob has a bunch of SVG files of varying sizes, and gives them to Alice, then Alice may have to manually resize each one, when using it, as suggested in this answer. It may be much easier to resize them all using a command-line script, so Alice can "plug and play", without having to individually resize each one to the needed dimensions. You're right, this is a workflow issue, but plenty of workflows don't automagically resize SVGs (mine doesn't). Commented May 8, 2021 at 1:51
  • ⚠️ Note: ImageMagick.org states it cannot losslessly resize SVGs, because there is an intermediate step: rasterization. See "A word about Vector Image formats" on Imagemagick.org, but not sure if this is still true as of 2021. Commented May 8, 2021 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.