rafaelcb21
rafaelcb21

Reputation: 13314

Inverted svg image with svgpathtools

I'm trying to plot an svg image but it's showing up inverted, would anyone know how to fix this?

Input

<img src="https://static.wikia.nocookie.net/ptstarwars/images/9/9d/Jedi_symbol.svg" alt="drawing" style="width:200px;"/>

Code

import svgpathtools
import numpy as np
import matplotlib.pyplot as plt

svgpaths, attributes = svgpathtools.svg2paths('./jedi_symbol.svg')
paths = []

for n, (attrs, svgpath) in enumerate(zip(attributes, svgpaths)):
    path = []
    for segment in svgpath:
        num_steps = int(segment.length()/0.5)
        tt = np.linspace(0, 1, num_steps)
        xy = np.array([segment.point(t) for t in tt])
        if len(xy) > 0:
            if path and xy[0] == path[-1]:
                xy = xy[1:]
        path.extend(xy)
    paths.append(np.array(path))

paths = np.array(paths)
x_table, y_table = paths[0].real, paths[0].imag

# Simple method to center the image
x_table = x_table - min(x_table)
y_table = y_table - min(y_table)
x_table = x_table - max(x_table) / 2
y_table = y_table - max(y_table) / 2    

fig, ax = plt.subplots(figsize=(5, 5))
ax.set_aspect('equal', 'datalim')
ax.plot(x_table, y_table)

Output

enter image description here

Upvotes: 1

Views: 141

Answers (1)

rafaelcb21
rafaelcb21

Reputation: 13314

It just multiply the y by -1

ax.plot(x_table, -1*y_table)

Upvotes: 1

Related Questions