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
Code for the Active Speakers in Context Paper (CVPR2020)

Active Speakers in Context This repo contains the official code and models for the "Active Speakers in Context" CVPR 2020 paper. Before Training The c

43 Oct 14, 2022
Source code and dataset for ACL2021 paper: "ERICA: Improving Entity and Relation Understanding for Pre-trained Language Models via Contrastive Learning".

ERICA Source code and dataset for ACL2021 paper: "ERICA: Improving Entity and Relation Understanding for Pre-trained Language Models via Contrastive L

THUNLP 75 Nov 02, 2022
A package, and script, to perform imaging transcriptomics on a neuroimaging scan.

Imaging Transcriptomics Imaging transcriptomics is a methodology that allows to identify patterns of correlation between gene expression and some prop

Alessio Giacomel 10 Dec 27, 2022
AOT (Associating Objects with Transformers) in PyTorch

An efficient modular implementation of Associating Objects with Transformers for Video Object Segmentation in PyTorch

162 Dec 14, 2022
Offical implementation of Shunted Self-Attention via Multi-Scale Token Aggregation

Shunted Transformer This is the offical implementation of Shunted Self-Attention via Multi-Scale Token Aggregation by Sucheng Ren, Daquan Zhou, Shengf

156 Dec 27, 2022
2021 credit card consuming recommendation

2021 credit card consuming recommendation

Wang, Chung-Che 7 Mar 08, 2022
Official code for "Eigenlanes: Data-Driven Lane Descriptors for Structurally Diverse Lanes", CVPR2022

[CVPR 2022] Eigenlanes: Data-Driven Lane Descriptors for Structurally Diverse Lanes Dongkwon Jin, Wonhui Park, Seong-Gyun Jeong, Heeyeon Kwon, and Cha

Dongkwon Jin 106 Dec 29, 2022
Pynomial - a lightweight python library for implementing the many confidence intervals for the risk parameter of a binomial model

Pynomial - a lightweight python library for implementing the many confidence intervals for the risk parameter of a binomial model

Demetri Pananos 9 Oct 04, 2022
Robust, modular and efficient implementation of advanced Hamiltonian Monte Carlo algorithms

AdvancedHMC.jl AdvancedHMC.jl provides a robust, modular and efficient implementation of advanced HMC algorithms. An illustrative example for Advanced

The Turing Language 167 Jan 01, 2023
Yolov5-opencv-cpp-python - Example of using ultralytics YOLO V5 with OpenCV 4.5.4, C++ and Python

yolov5-opencv-cpp-python Example of performing inference with ultralytics YOLO V

183 Jan 09, 2023
CVPR2021 Content-Aware GAN Compression

Content-Aware GAN Compression [ArXiv] Paper accepted to CVPR2021. @inproceedings{liu2021content, title = {Content-Aware GAN Compression}, auth

52 Nov 06, 2022
Trafffic prediction analysis using hybrid models - Machine Learning

Hybrid Machine learning Model Clone the Repository Create a new Directory as assests and download the model from the below link Model Link To Start th

1 Feb 08, 2022
PyTorch code for EMNLP 2021 paper: Don't be Contradicted with Anything! CI-ToD: Towards Benchmarking Consistency for Task-oriented Dialogue System

PyTorch code for EMNLP 2021 paper: Don't be Contradicted with Anything! CI-ToD: Towards Benchmarking Consistency for Task-oriented Dialogue System

Libo Qin 25 Sep 06, 2022
This is an official implementation of our CVPR 2021 paper "Bottom-Up Human Pose Estimation Via Disentangled Keypoint Regression" (https://arxiv.org/abs/2104.02300)

Bottom-Up Human Pose Estimation Via Disentangled Keypoint Regression Introduction In this paper, we are interested in the bottom-up paradigm of estima

HRNet 367 Dec 27, 2022
Using contrastive learning and OpenAI's CLIP to find good embeddings for images with lossy transformations

Creating Robust Representations from Pre-Trained Image Encoders using Contrastive Learning Sriram Ravula, Georgios Smyrnis This is the code for our pr

Sriram Ravula 26 Dec 10, 2022
Train a deep learning net with OpenStreetMap features and satellite imagery.

DeepOSM Classify roads and features in satellite imagery, by training neural networks with OpenStreetMap (OSM) data. DeepOSM can: Download a chunk of

TrailBehind, Inc. 1.3k Nov 24, 2022
Official code of the paper "Expanding Low-Density Latent Regions for Open-Set Object Detection" (CVPR 2022)

OpenDet Expanding Low-Density Latent Regions for Open-Set Object Detection (CVPR2022) Jiaming Han, Yuqiang Ren, Jian Ding, Xingjia Pan, Ke Yan, Gui-So

csuhan 64 Jan 07, 2023
This repository contains the source code for the paper First Order Motion Model for Image Animation

!!! Check out our new paper and framework improved for articulated objects First Order Motion Model for Image Animation This repository contains the s

13k Jan 09, 2023
Cowsay - A rewrite of cowsay in python

Python Cowsay A rewrite of cowsay in python. Allows for parsing of existing .cow

James Ansley 3 Jun 27, 2022
PyTorch implementation of MoCo v3 for self-supervised ResNet and ViT.

MoCo v3 for Self-supervised ResNet and ViT Introduction This is a PyTorch implementation of MoCo v3 for self-supervised ResNet and ViT. The original M

Facebook Research 887 Jan 08, 2023