Agile SVG maker for python

Related tags

Deep LearningASVG
Overview

Agile SVG Maker

Need to draw hundreds of frames for a GIF? Need to change the style of all pictures in a PPT? Need to draw similar images with different parameters? Try ASVG!

Under construction, not so agile yet...

Basically aimed at academic illustrations.

Simple Example

from ASVG import *

# A 500x300 canvas
a = Axis((500, 300)) 

# Draw a rectangle on a, at level 1, from (0,0) to (200,100)
# With (5,5) round corner, fill with red color.
rect(a, 1, 0, 0, 200, 100, 5, 5, fill='red')

# Draw a circle on a, at level 3
# Centered (50,50) with 50 radius, fill with blue color.
circle(a, 3, 50, 50, 50, fill='blue')

# Draw this picture to example.svg
draw(a, "example.svg")

Parameterized Sub-image

def labeledRect(
        level: int,
        width: float,
        height: float,
        s: Union[str, TextRepresent],
        font_size: float,
        textShift: Tuple[float, float] = (0, 0),
        font: str = "Arial",
        rx: float = 0,
        ry: float = 0,
        margin: float = 5,
        attrib: Attrib = Attrib(),
        rectAttrib: Attrib = Attrib(),
        textAttrib: Attrib = Attrib(),
        **kwargs):
    e = ComposedElement((width + 2 * margin, height + 2 * margin),
                        level, attrib + kwargs)
    rect(e, 0, margin, margin, width, height, rx, ry, attrib=rectAttrib)

    textX = width / 2 + textShift[0] + margin
    textY = height / 2 + textShift[1] + (font_size / 2) + margin
    text(e, 1, s, textX, textY, font_size, font, attrib=textAttrib)
    return e

a = Axis((300,200))
a.addElement(labeledRect(...))

Nested Canvas

Canvas and Axis

Create a canvas axis with Axis(size, viewport) size=(width, height) is the physical size of the canvas in pixels. viewport=(x, y) is the logical size of the axis, by default its the same of the physical size.

# A 1600x900 canvas, axis range [0,1600)x[0,900)
a = Axis((1600, 900))

# A 1600x900 canva, with normalized axis range[0,1),[0,1)
b = Axis((1600, 900), (1.0, 1.0))

ComposedElement

A composed element is a sub-image.

ComposedElement(size, level, attrib) size=(width, height): the size of the axis of this element. level: the higher the level is, the fronter the composed element is. attrib: the common attributes of this element

Add a composed element into the big canvas:axis.addElement(element, shift) shift=(x,y) is the displacement of the element in the outer axis.

A composed element can have other composed elements as sub-pictures: element.addElement(subElement, shift)

Basic Elements

The basic element comes from SVG. Basicly, every element needs a axis and a level argument. axis can be a Axis or ComposedElement. The bigger the level is, the fronter the element is. level is only comparable when two elements are under the same axis.

# Rectangle
rect(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    x: float, # top left
    y: float,
    width: float,
    height: float,
    rx: float = 0.0, # round corner radius
    ry: float = 0.0,
    attrib: core.Attrib = core.Attrib(),
    **kwargs
)
# Circle
circle(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    cx: float, # center
    cy: float,
    r: float, # radius
    attrib: core.Attrib = core.Attrib(),
    **kwargs
)
# Ellipse
ellipse(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    cx: float, # center
    cy: float,
    rx: float, # radius
    ry: float,
    attrib: core.Attrib = core.Attrib(),
    **kwargs
)
# Straight line
line(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    x1: float, # Start
    y1: float,
    x2: float, # End
    y2: float,
    attrib: core.Attrib = core.Attrib(),
    **kwargs
)
# Polyline
polyline(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    points: List[Tuple[float, float]],
    attrib: core.Attrib = core.Attrib(),
    **kwargs
)
# Polygon
polygon(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    points: List[Tuple[float, float]],
    attrib: core.Attrib = core.Attrib(),
    **kwargs
)
# Path
path(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    d: PathD,
    attrib: core.Attrib = core.Attrib(),
    **kwargs
)

PathD is a sequence of path descriptions, the actions is like SVG's path element. View Path tutorial We use ?To() for captial letters and ?For() for lower-case letters. close() and open() is for closing or opening the path. Example:

d = PathD()
d.moveTo(100,100)
d.hlineFor(90)
d.close()
# Equivilent: d = PathD(["M 80 80", "h 90",  "Z"])

path(a, 0, d)

Text

text(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    s: Union[str, TextRepresent],
    x: float,
    y: float,
    fontSize: int,
    font: str = "Arial",
    anchor: str = "middle",
    attrib: core.Attrib = core.Attrib(),
    **kwargs
)

anchor is where (x,y) is in the text. Can be either start, middle or end.

TextRepresent means formatted text. Normal string with \n in it will be converted into multilines. You can use TextSpan to add some attributes to a span of text.

Examples:

text(
    a, 10,
    "Hello\n???" + \
    TextSpan("!!!\n", fill='#00ffff', font_size=25) +\
    "???\nabcdef",
    30, 30, 20, anchor="start")

Arrow

# Straight arrow
arrow(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    x: float, # Position of the tip
    y: float,
    fromX: float, # Position of the other end
    fromY: float,
    tipSize: float = 10.0,
    tipAngle: float = 60.0,
    tipFilled: bool = True,
    **kwargs
)
# Polyline arrow
polyArrow(
    axis: Union[core.Axis, core.ComposedElement],
    level: int,
    points: List[Tuple[float, float]],
    tipSize: float = 10.0,
    tipAngle: float = 60.0,
    tipFilled: bool = True,
    **kwargs
)

Attributes

Attributes is for customizing the style of the elements.

myStyle = Attrib(
    fill = "#1bcd20",
    stroke = "black",
    stroke_width = "1pt"
)

alertStype = myStyle.copy()
alertStype.fill = "#ff0000"

rect(..., attrib=myStyle)
circle(..., attrib=alertStyle)

The name of the attribute are the same as in SVG elements, except use underline _ instead of dash -

Attributs of ComposedElement applies on <group> element.

For convinent, you can directly write some attributes in **kwargs.

rect(..., fill="red")

# Equivilient
rect(..., attrib=Attrib(fill="red))
Owner
SemiWaker
A student in Peking University Department of Electronic Engineering and Computer Science, Major in Artificial Intelligence.
SemiWaker
pyspark🍒🥭 is delicious,just eat it!😋😋

如何用10天吃掉pyspark? 🔥 🔥 《10天吃掉那只pyspark》 🚀

lyhue1991 578 Dec 30, 2022
Code implementation of Data Efficient Stagewise Knowledge Distillation paper.

Data Efficient Stagewise Knowledge Distillation Table of Contents Data Efficient Stagewise Knowledge Distillation Table of Contents Requirements Image

IvLabs 112 Dec 02, 2022
Code accompanying the paper Say As You Wish: Fine-grained Control of Image Caption Generation with Abstract Scene Graphs (Chen et al., CVPR 2020, Oral).

Say As You Wish: Fine-grained Control of Image Caption Generation with Abstract Scene Graphs This repository contains PyTorch implementation of our pa

Shizhe Chen 178 Dec 29, 2022
Escaping the Gradient Vanishing: Periodic Alternatives of Softmax in Attention Mechanism

Period-alternatives-of-Softmax Experimental Demo for our paper 'Escaping the Gradient Vanishing: Periodic Alternatives of Softmax in Attention Mechani

slwang9353 0 Sep 06, 2021
A simple log parser and summariser for IIS web server logs

IISLogFileParser A basic parser tool for IIS Logs which summarises findings from the log file. Inspired by the Gist https://gist.github.com/wh13371/e7

2 Mar 26, 2022
ConvMixer unofficial implementation

ConvMixer ConvMixer 非官方实现 pytorch 版本已经实现。 nets 是重构版本 ,test 是官方代码 感兴趣小伙伴可以对照看一下。 keras 已经实现 tf2.x 中 是tensorflow 2 版本 gelu 激活函数要求 tf=2.4 否则使用入下代码代替gelu

Jian Tengfei 8 Jul 11, 2022
Supplementary code for the experiments described in the 2021 ISMIR submission: Leveraging Hierarchical Structures for Few Shot Musical Instrument Recognition.

Music Trees Supplementary code for the experiments described in the 2021 ISMIR submission: Leveraging Hierarchical Structures for Few Shot Musical Ins

Hugo Flores García 32 Nov 22, 2022
Dark Finix: All in one hacking framework with almost 100 tools

Dark Finix - Hacking Framework. Dark Finix is a all in one hacking framework wit

Md. Nur habib 2 Feb 18, 2022
ProjectOxford-ClientSDK - This repo has moved :house: Visit our website for the latest SDKs & Samples

This project has moved 🏠 We heard your feedback! This repo has been deprecated and each project has moved to a new home in a repo scoped by API and p

Microsoft 970 Nov 28, 2022
Python scripts performing class agnostic object localization using the Object Localization Network model in ONNX.

ONNX Object Localization Network Python scripts performing class agnostic object localization using the Object Localization Network model in ONNX. Ori

Ibai Gorordo 15 Oct 14, 2022
GalaXC: Graph Neural Networks with Labelwise Attention for Extreme Classification

GalaXC GalaXC: Graph Neural Networks with Labelwise Attention for Extreme Classification @InProceedings{Saini21, author = {Saini, D. and Jain,

Extreme Classification 28 Dec 05, 2022
Deep learning operations reinvented (for pytorch, tensorflow, jax and others)

This video in better quality. einops Flexible and powerful tensor operations for readable and reliable code. Supports numpy, pytorch, tensorflow, and

Alex Rogozhnikov 6.2k Jan 01, 2023
JAXDL: JAX (Flax) Deep Learning Library

JAXDL: JAX (Flax) Deep Learning Library Simple and clean JAX/Flax deep learning algorithm implementations: Soft-Actor-Critic (arXiv:1812.05905) Transf

Patrick Hart 4 Nov 27, 2022
Region-aware Contrastive Learning for Semantic Segmentation, ICCV 2021

Region-aware Contrastive Learning for Semantic Segmentation, ICCV 2021 Abstract Recent works have made great success in semantic segmentation by explo

Hanzhe Hu 30 Dec 29, 2022
LF-YOLO (Lighter and Faster YOLO) is used to detect defect of X-ray weld image.

This project is based on ultralytics/yolov3. LF-YOLO (Lighter and Faster YOLO) is used to detect defect of X-ray weld image. Download $ git clone http

26 Dec 13, 2022
custom pytorch implementation of MoCo v3

MoCov3-pytorch custom implementation of MoCov3 [arxiv]. I made minor modifications based on the official MoCo repository [github]. No ViT part code an

39 Nov 14, 2022
RTS3D: Real-time Stereo 3D Detection from 4D Feature-Consistency Embedding Space for Autonomous Driving

RTS3D: Real-time Stereo 3D Detection from 4D Feature-Consistency Embedding Space for Autonomous Driving (AAAI2021). RTS3D is efficiency and accuracy s

71 Nov 29, 2022
Official PyTorch code for Mutual Affine Network for Spatially Variant Kernel Estimation in Blind Image Super-Resolution (MANet, ICCV2021)

Mutual Affine Network for Spatially Variant Kernel Estimation in Blind Image Super-Resolution (MANet, ICCV2021) This repository is the official PyTorc

Jingyun Liang 139 Dec 29, 2022
根据midi文件演奏“风物之诗琴”的脚本 "Windsong Lyre" auto play

Genshin-lyre-auto-play 简体中文 | English 简介 根据midi文件演奏“风物之诗琴”的脚本。由Python驱动,在此承诺, ⚠️ 项目内绝不含任何能够引起安全问题的代码。 前排提示:所有键盘在动但是原神没反应的都是因为没有管理员权限,双击run.bat或者以管理员模式

御坂17032号 386 Jan 01, 2023
Accommodating supervised learning algorithms for the historical prices of the world's favorite cryptocurrency and boosting it through LightGBM.

Accommodating supervised learning algorithms for the historical prices of the world's favorite cryptocurrency and boosting it through LightGBM.

1 Nov 27, 2021