Skip to content

Instantly share code, notes, and snippets.

@K4zuki
Created August 28, 2017 06:02
Show Gist options
  • Save K4zuki/33c9791ea60166832a72b315a016c6b1 to your computer and use it in GitHub Desktop.
Save K4zuki/33c9791ea60166832a72b315a016c6b1 to your computer and use it in GitHub Desktop.
python3 script to rotate svg by degree
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# simple SVG rotate script
# great hint from
# https://stackoverflow.com/questions/43199869/rotate-and-scale-a-complete-svg-document-using-python
# MIT license (c) K4ZUKI(k.yamamoto.08136891@gmail.com)
"""
import svgutils
import math
def calc(x, y, deg):
rad = math.radians(deg)
newx = float(x) * abs(math.cos(rad)) + float(y) * abs(math.sin(rad))
newy = float(x) * abs(math.sin(rad)) + float(y) * abs(math.cos(rad))
return (newx, newy)
def main():
angle = 90
svg = svgutils.transform.fromfile('24530f17.svg')
originalSVG = svgutils.compose.SVG('24530f17.svg')
originalSVG.rotate(angle)
offset_x = float(svg.height) * abs(math.sin(math.radians(angle)))
originalSVG.move(offset_x, 0)
print(calc(svg.width, svg.height, angle))
newx, newy = calc(svg.width, svg.height, angle)
figure = svgutils.compose.Figure(
newx, newy,
originalSVG)
figure.save('svgNew.svg')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment