user7720342
user7720342

Reputation:

Rotate and scale a complete .SVG document using Python

I have a SVG drawing (from a Building Map) and I want to rotate the complete document 90 degrees clockwise. Now, the drawing orientation is portrait, the idea is to have a landscape orientation.

Besides of this, I would like to scale the complete document (so including all elements).

For now, I could not find the possibilties for doing this on the web. So that is why I am asking overhere. My questions are:

  1. Is it possible?
  2. If yes, in what way can this be done? And who wants to help with this issue.

Upvotes: 3

Views: 8368

Answers (1)

Andrew Che
Andrew Che

Reputation: 968

I managed to rotate an SVG figure with svgutils. It can be installed from PyPI,

pip install svgutils
import svgutils
svg = svgutils.transform.fromfile('camera.svg')
originalSVG = svgutils.compose.SVG('camera.svg')
originalSVG.rotate(90)
originalSVG.move(svg.height, 10)
figure = svgutils.compose.Figure(svg.height, svg.width, originalSVG)
figure.save('svgNew.svg')

Note that width and height attributes must be specified in original svg file in svg tag.

Actually, this method didn't do anything with elements except wrapping them all with g tag with transform attribute. But it seems that with this module you can access each and every element in SVG tree and do whatever you want with them.

Scaling an SVG is also easy:

originalSVG.scale(2)
figure = svgutils.compose.Figure(float(svg.height) * 2, float(svg.width) * 2, originalSVG)
figure.save('svgNew.svg')

Upvotes: 9

Related Questions