예제 #1
0
def parse_linear_gradient(node, transform, defs):
    begin = torch.tensor([0.0, 0.0])
    end = torch.tensor([0.0, 0.0])
    offsets = []
    stop_colors = []
    # Inherit from parent
    for key in node.attrib:
        if remove_namespaces(key) == 'href':
            value = node.attrib[key]
            parent = defs[value.lstrip('#')]
            begin = parent.begin
            end = parent.end
            offsets = parent.offsets
            stop_colors = parent.stop_colors

    for attrib in node.attrib:
        attrib = remove_namespaces(attrib)
        if attrib == 'x1':
            begin[0] = float(node.attrib['x1'])
        elif attrib == 'y1':
            begin[1] = float(node.attrib['y1'])
        elif attrib == 'x2':
            end[0] = float(node.attrib['x2'])
        elif attrib == 'y2':
            end[1] = float(node.attrib['y2'])
        elif attrib == 'gradientTransform':
            transform = transform @ parse_transform(node.attrib['gradientTransform'])

    begin = transform @ torch.cat((begin, torch.ones([1])))
    begin = begin / begin[2]
    begin = begin[:2]
    end = transform @ torch.cat((end, torch.ones([1])))
    end = end / end[2]
    end = end[:2]

    for child in node:
        tag = remove_namespaces(child.tag)
        if tag == 'stop':
            offset = float(child.attrib['offset'])
            color = [0.0, 0.0, 0.0, 1.0]
            if 'stop-color' in child.attrib:
                c = parse_color(child.attrib['stop-color'], defs)
                color[:3] = [c[0], c[1], c[2]]
            if 'stop-opacity' in child.attrib:
                color[3] = float(child.attrib['stop-opacity'])
            if 'style' in child.attrib:
                style = parse_style(child.attrib['style'], defs)
                if 'stop-color' in style:
                    c = parse_color(style['stop-color'], defs)
                    color[:3] = [c[0], c[1], c[2]]
                if 'stop-opacity' in style:
                    color[3] = float(style['stop-opacity'])
            offsets.append(offset)
            stop_colors.append(color)
    if isinstance(offsets, list):
        offsets = torch.tensor(offsets)
    if isinstance(stop_colors, list):
        stop_colors = torch.tensor(stop_colors)

    return pydiffvg.LinearGradient(begin, end, offsets, stop_colors)
예제 #2
0
import pydiffvg
import torch
import skimage
import numpy as np

# Use GPU if available
pydiffvg.set_use_gpu(torch.cuda.is_available())

canvas_width, canvas_height = 256, 256
color = pydiffvg.LinearGradient(\
    begin = torch.tensor([50.0, 50.0]),
    end = torch.tensor([200.0, 200.0]),
    offsets = torch.tensor([0.0, 1.0]),
    stop_colors = torch.tensor([[0.2, 0.5, 0.7, 1.0],
                                [0.7, 0.2, 0.5, 1.0]]))
circle = pydiffvg.Circle(radius = torch.tensor(40.0),
                         center = torch.tensor([128.0, 128.0]))
shapes = [circle]
circle_group = pydiffvg.ShapeGroup(shape_ids = torch.tensor([0]), fill_color = color)
shape_groups = [circle_group]
scene_args = pydiffvg.RenderFunction.serialize_scene(\
    canvas_width, canvas_height, shapes, shape_groups)

render = pydiffvg.RenderFunction.apply
img = render(256, # width
             256, # height
             2,   # num_samples_x
             2,   # num_samples_y
             0,   # seed
             None, # background_image
             *scene_args)