Agile SVG maker for python

Related tags

Deep Learning ASVG
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))
You might also like...
This is an open source python repository for various python tests

Welcome to Py-tests This is an open source python repository for various python tests. This is in response to the hacktoberfest2021 challenge. It is a

Composable transformations of Python+NumPy programsComposable transformations of Python+NumPy programs

Chex Chex is a library of utilities for helping to write reliable JAX code. This includes utils to help: Instrument your code (e.g. assertions) Debug

Automatic self-diagnosis program (python required)Automatic self-diagnosis program (python required)

auto-self-checker 자동으로 자가진단 해주는 프로그램(python 필요) 중요 이 프로그램이 실행될때에는 절대로 마우스포인터를 움직이거나 키보드를 건드리면 안된다(화면인식, 마우스포인터로 직접 클릭) 사용법 프로그램을 구동할 폴더 내의 cmd창에서 pip

POPPY (Physical Optics Propagation in Python) is a Python package that simulates physical optical propagation including diffraction
POPPY (Physical Optics Propagation in Python) is a Python package that simulates physical optical propagation including diffraction

POPPY: Physical Optics Propagation in Python POPPY (Physical Optics Propagation in Python) is a Python package that simulates physical optical propaga

Space-invaders - Simple Game created using Python & PyGame, as my Beginner Python Project
Space-invaders - Simple Game created using Python & PyGame, as my Beginner Python Project

Space Invaders This is a simple SPACE INVADER game create using PYGAME whihc hav

Snapchat-filters-app-opencv-python - Here we used opencv and other inbuilt python modules to create filter application like snapchat Yolov5-opencv-cpp-python - Example of using ultralytics YOLO V5 with OpenCV 4.5.4, C++ and Python
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

Python-kafka-reset-consumergroup-offset-example - Python Kafka reset consumergroup offset example

Python Kafka reset consumergroup offset example This is a simple example of how

Experimental Python implementation of OpenVINO Inference Engine (very slow, limited functionality). All codes are written in Python. Easy to read and modify.
Experimental Python implementation of OpenVINO Inference Engine (very slow, limited functionality). All codes are written in Python. Easy to read and modify.

PyOpenVINO - An Experimental Python Implementation of OpenVINO Inference Engine (minimum-set) Description The PyOpenVINO is a spin-off product from my

Owner
SemiWaker
A student in Peking University Department of Electronic Engineering and Computer Science, Major in Artificial Intelligence.
SemiWaker
AITUS - An atomatic notr maker for CYTUS

AITUS an automatic note maker for CYTUS. 利用AI根据指定乐曲生成CYTUS游戏谱面。 效果展示:https://www

GradiusTwinbee 6 Feb 24, 2022
Annotated notes and summaries of the TensorFlow white paper, along with SVG figures and links to documentation

TensorFlow White Paper Notes Features Notes broken down section by section, as well as subsection by subsection Relevant links to documentation, resou

Sam Abrahams 437 Oct 9, 2022
SVG Icon processing tool for C++

BAWR This is a tool to automate the icons generation from sets of svg files into fonts and atlases. The main purpose of this tool is to add it to the

Frank David Martínez M 66 Dec 14, 2022
tinykernel - A minimal Python kernel so you can run Python in your Python

tinykernel - A minimal Python kernel so you can run Python in your Python

fast.ai 37 Dec 2, 2022
Python-experiments - A Repository which contains python scripts to automate things and make your life easier with python

Python Experiments A Repository which contains python scripts to automate things

Vivek Kumar Singh 11 Sep 25, 2022
Crab is a flexible, fast recommender engine for Python that integrates classic information filtering recommendation algorithms in the world of scientific Python packages (numpy, scipy, matplotlib).

Crab - A Recommendation Engine library for Python Crab is a flexible, fast recommender engine for Python that integrates classic information filtering r

python-recsys 1.2k Dec 21, 2022
Python scripts to detect faces in Python with the BlazeFace Tensorflow Lite models

Python scripts to detect faces using Python with the BlazeFace Tensorflow Lite models. Tested on Windows 10, Tensorflow 2.4.0 (Python 3.8).

Ibai Gorordo 46 Nov 17, 2022
A fast python implementation of Ray Tracing in One Weekend using python and Taichi

ray-tracing-one-weekend-taichi A fast python implementation of Ray Tracing in One Weekend using python and Taichi. Taichi is a simple "Domain specific

null 157 Dec 26, 2022
Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! Very tiny! Stock Market Financial Technical Analysis Python library . Quant Trading automation or cryptocoin exchange

MyTT Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! to Stock Market Financial Technical Analysis Python

dev 34 Dec 27, 2022