How to use svgutils - 10 common examples

To help you get started, we’ve selected a few svgutils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github btel / svg_utils / tests / test_transform.py View on Github external
def test_group_class():
    svg_fig = transform.fromstring(circle)
    group = svg_fig.getroot()
    ok_((group.root.attrib['class'] == 'main'))
github btel / svg_utils / tests / test_transform.py View on Github external
def test_skew():
    svg_fig = transform.fromstring(circle)
    group = svg_fig.getroot()

    # Test skew in y-axis
    group.skew(0, 30)
    ok_('skewY(30' in group.root.get('transform'))

    # Test skew in x-axis
    group.skew(30, 0)
    ok_('skewX(30' in group.root.get('transform'))
github btel / svg_utils / tests / test_transform.py View on Github external
def test_get_size():
    svg_fig = transform.fromstring(circle)
    w, h = svg_fig.get_size()
    ok_((w=='150') & (h=='50'))
github btel / svg_utils / tests / test_transform.py View on Github external
def test_scale_xy():
    svg_fig = transform.fromstring(circle)
    group = svg_fig.getroot()

    group.scale_xy(0, 30)
    ok_('scale(0' in group.root.get('transform'))
github btel / svg_utils / tests / test_compose.py View on Github external
def test_text():

    fig = Figure("5cm", "5cm",
                 Text('lion')
                 )
    txt = fig.root.find(SVG+'text')

    ok_(txt.text=='lion')
github btel / svg_utils / tests / test_compose.py View on Github external
def test_embedded_image():
    lion_jpg_md5 = 'f4a7c2a05f2acefa50cbd75a32d2733c'

    fig = Figure("5cm", "5cm",
                 Image(120, 120,
                       'examples/files/lion.jpeg')
                 )
    image = fig.root.find(SVG+'image')
    image_data = image.attrib[XLINK+'href']
    image_data = image_data.replace('data:image/jpeg;base64,', '').encode('ascii')
    base64 = codecs.decode(image_data, 'base64')
    md5 = hashlib.md5(base64).hexdigest()

    ok_(lion_jpg_md5 == md5)
github poldracklab / niworkflows / niworkflows / viz / utils.py View on Github external
viewbox = [float(v) for v in f.root.get("viewBox").split(" ")]
        width = int(viewbox[2])
        height = int(viewbox[3])
        sizes.append((width, height))
    nsvgs = len(bg_svgs)

    sizes = np.array(sizes)

    # Calculate the scale to fit all widths
    width = sizes[ref, 0]
    scales = width / sizes[:, 0]
    heights = sizes[:, 1] * scales

    # Compose the views panel: total size is the width of
    # any element (used the first here) and the sum of heights
    fig = SVGFigure(width, heights[:nsvgs].sum())

    yoffset = 0
    for i, r in enumerate(roots):
        r.moveto(0, yoffset, scale=scales[i])
        if i == (nsvgs - 1):
            yoffset = 0
        else:
            yoffset += heights[i]

    # Group background and foreground panels in two groups
    if fg_svgs:
        newroots = [
            GroupElement(roots[:nsvgs], {"class": "background-svg"}),
            GroupElement(roots[nsvgs:], {"class": "foreground-svg"}),
        ]
    else:
github btel / svg_utils / docs / source / tutorials / fig_final.py View on Github external
import svgutils.transform as sg
import sys 

#create new SVG figure
fig = sg.SVGFigure("16cm", "6.5cm")

# load matpotlib-generated figures
fig1 = sg.fromfile('sigmoid_fit.svg')
fig2 = sg.fromfile('anscombe.svg')

# get the plot objects
plot1 = fig1.getroot()
plot2 = fig2.getroot()
plot2.moveto(280, 0, scale=0.5)

# add text labels
txt1 = sg.TextElement(25,20, "A", size=12, weight="bold")
txt2 = sg.TextElement(305,20, "B", size=12, weight="bold")

# append plots and labels to figure
fig.append([plot1, plot2])
fig.append([txt1, txt2])

# save generated SVG files
fig.save("fig_final.svg")
github gkichaev / PAINTOR_V3.0 / CANVIS / CANVIS.py View on Github external
if heatmaps == None:
        horizontal = 'n'
    elif len(heatmaps)>1:
        horizontal='y'

    if horizontal == 'y':
        size_width = "9in"
        size_height = '9in'
    else:
        size_width = "5in"
        size_height = '11in'

    fig = sg.SVGFigure(size_width, size_height)
    value_plots.savefig('value_plots.svg', format='svg', dpi=DPI, transparent=True)
    value_plots = sg.fromfile('value_plots.svg')
    plot1 = value_plots.getroot()
    if annotation_plot is not None:
        len_ann_plot = (len(annotation_plot))
    else:
        len_ann_plot = 0
    plot1.moveto(0, 0)
    fig.append(plot1)
    # plot heatmap(s)
    len_annotation_plot = size_annotation_plot * (len_ann_plot + 1)
    if heatmaps is not None:
        heatmap_count = 0
        for heatmap in heatmaps:
            plot4 = heatmap[0]
            try:
                plot4.savefig('heatmap.svg', format='svg', dpi=DPI, transparent=True)
                plot4 = sg.fromfile('heatmap.svg')
github chrisjbillington / git_nautilus_icons / icons / generate_icons.py View on Github external
try:
    shutil.rmtree('hicolor')
except FileNotFoundError:
    pass
    
os.system('mkdir -p hicolor/scalable/emblems/')
for tl, tr, bl, br in all_icons:
    # create new SVG figure
    background_image = sg.SVGFigure(32, 32)
    if bl is not None:
        bl_file = 'sub_icons/{}-l.svg'.format(bl)
        bl_image = sg.fromfile(bl_file).getroot()
        background_image.append(bl_image)
    if br is not None:
        br_file = 'sub_icons/{}-r.svg'.format(br)
        br_image = sg.fromfile(br_file).getroot()
        background_image.append(br_image)
    if tr is not None:
        tr_file = 'sub_icons/{}.svg'.format(tr)
        tr_image = sg.fromfile(tr_file).getroot()
        background_image.append(tr_image)
    if tl is not None:
        tl_file = 'sub_icons/{}.svg'.format(tl)
        tl_image = sg.fromfile(tl_file).getroot()
        background_image.append(tl_image)
    filename = '-'.join([name for name in (tl, tr, bl, br) if name is not None])
    filename = 'hicolor/scalable/emblems/git-{}.svg'.format(filename)
    background_image.save(filename)

# Simplified icons for the 16x16 size. Only shows a single icon for the worktree part of
# the status.
os.system('mkdir -p hicolor/16x16/emblems/')