Move, Rotate, and Scale SVG Elements in Python

Quick Answer: To transform SVG elements in Python, load the SVG with SVGDocument, find the target element with selectors or tag-name search, set the transform attribute with set_attribute(), and save the edited SVG.

SVG Transform Basics

SVG transformations change how an element is rendered without rewriting its original geometry. This is useful when you need to move an icon, rotate a shape, scale a group, reposition generated artwork, or preserve the original coordinates while changing the rendered result.

In Aspose.SVG for Python via .NET, transformations are edited as SVG transform attribute values. Use Element.set_attribute() to write a complete transform value such as translate(...), rotate(...), scale(...), or a combined transform list.

Common transform functions are:

Transform valueExampleAspose.SVG API pattern
translate(tx ty)translate(40 12)Write with Element.set_attribute() as the transform value
rotate(angle)rotate(30)Set transform on the selected SVG element
rotate(angle cx cy)rotate(30 190 68)Set transform with an explicit rotation center
scale(s)scale(1.2)Set transform when x and y scale equally
scale(sx sy)scale(1.2 0.8)Set transform when x and y scale separately
transform listtranslate(20 0) rotate(15)Write a complete ordered list of transform operations

This article uses the sample file transform-svg-elements.svg for the code examples and illustrations. Use it as data/transform-svg-elements.svg when running the examples.

Move an SVG Element

Use translate(tx ty) when you need to move an element without changing its x, y, cx, cy, or path data. The example below finds the #badge group, moves it 25 units to the left and 25 units up, and saves the edited SVG.

 1import os
 2from aspose.svg import SVGDocument
 3
 4input_folder = "data/"
 5output_folder = "output/"
 6input_path = os.path.join(input_folder, "transform-svg-elements.svg")
 7output_path = os.path.join(output_folder, "badge-moved.svg")
 8os.makedirs(output_folder, exist_ok=True)
 9
10with SVGDocument(input_path) as document:
11    badge = document.root_element.query_selector("#badge")
12
13    if badge is not None:
14        badge.set_attribute("transform", "translate(-25 -25)")
15
16    document.save(output_path)

The code uses query_selector() to find the group by ID, updates its transform attribute, and writes the result with SVGDocument.save(). Applying the transform to a <g> element moves all shapes inside the group together.

The illustration below compares the source SVG (a) with the result after applying translate(-25 -25) to the #badge group (b).

Original SVG and moved badge group after applying translate transform

Rotate an SVG Element Around a Point

Use rotate(angle cx cy) when the element should rotate around its own center or another known point. The example rotates the #diamond rectangle around the center of its original bounding area.

 1import os
 2from aspose.svg import SVGDocument
 3
 4input_folder = "data/"
 5output_folder = "output/"
 6input_path = os.path.join(input_folder, "transform-svg-elements.svg")
 7output_path = os.path.join(output_folder, "rectangle-rotated.svg")
 8os.makedirs(output_folder, exist_ok=True)
 9
10with SVGDocument(input_path) as document:
11    diamond = document.root_element.query_selector("#diamond")
12
13    if diamond is not None:
14        diamond.set_attribute("transform", "rotate(45 195 60)")
15
16    document.save(output_path)

The center point in rotate(45 195 60) is part of the transform value. Without that point, SVG rotates the element around the current coordinate origin, which often makes the element appear to move unexpectedly.

The illustration below shows the rectangle before (a) and after rotation around the point defined in rotate(45 195 60) (b).

Original SVG and rectangle rotated around a specified point

Scale an SVG Group

Use scale() when you need to resize an element or group. Because scaling happens around the current origin, combine translate() and scale() when you want the scaled result to stay in a useful position.

The example below finds the #arrow group and sets a complete transform value: translate(250 70) scale(1.5). The translation places the arrow inside the SVG viewport, and the scale operation makes the change easy to see.

 1import os
 2from aspose.svg import SVGDocument
 3
 4input_folder = "data/"
 5output_folder = "output/"
 6input_path = os.path.join(input_folder, "transform-svg-elements.svg")
 7output_path = os.path.join(output_folder, "arrow-scaled.svg")
 8os.makedirs(output_folder, exist_ok=True)
 9
10with SVGDocument(input_path) as document:
11    arrow = document.root_element.query_selector("#arrow")
12
13    if arrow is not None:
14        arrow.set_attribute("transform", "translate(250 70) scale(1.5)")
15
16    document.save(output_path)

This example intentionally replaces the previous transform value on #arrow. Use this approach when you want the output to have a known final position and size.

The illustration below compares the source SVG (a) with the result after applying translate(250 70) scale(1.5) to the #arrow group (b).

Original SVG and scaled arrow group after applying translate and scale transforms

Common Mistakes and Fixes

ProblemLikely causeFix
The element rotates and shifts awayrotate(angle) was used without a center pointUse rotate(angle cx cy) when the rotation should happen around a known point
Existing layout changes unexpectedlyA previous transform value was overwrittenReplace the whole value only when you want a known final transform
Scaling moves the elementScaling happens relative to the current originCombine translate() and scale() deliberately, and check transform order
Scaled element appears outside the viewportThe transform does not include a suitable translationCombine translate() and scale() so the scaled result stays visible
The saved SVG is unchangedThe document was not saved after editingCall document.save(output_path) after transform updates
The visual result differs from the transform stringTransform functions are applied in orderTest the exact order, for example translate(...) scale(...) versus scale(...) translate(...)

FAQ

How do I move an SVG element in Python?

Load the SVG with SVGDocument, find the element with query_selector() or get_elements_by_tag_name(), set element.set_attribute("transform", "translate(40 12)"), and save the document.

How do I rotate an SVG element around its center?

Set a transform such as rotate(45 195 60), where 45 is the angle and 195 60 is the rotation center. Use a center point to avoid rotating around the SVG origin by accident.

How do I scale an SVG element in Python?

Set the transform attribute to scale(1.5) for simple scaling, or use a combined value such as translate(250 70) scale(1.5) when the scaled element also needs to stay in a specific position.

How do I combine translate and scale in SVG?

Set one transform value that contains both operations, for example element.set_attribute("transform", "translate(250 70) scale(1.5)"). The order of operations affects the final rendered result.

Should I change x and y attributes or use transform?

Use x, y, cx, or cy for simple geometry edits on one shape. Use transform when you need to move, rotate, scale, or preserve the original geometry while changing how the element is rendered.

Related Articles