analitics

Pages

Showing posts with label svgwrite. Show all posts
Showing posts with label svgwrite. Show all posts

Sunday, July 13, 2025

Python Qt6 : simple celtic knots tool with SVG file format.

Today, I try to create SVG file with an celtic knot design tool.
I used random values from -360 up to 360 for for Twist 1, Twist 2, and Twist 3 sliders.
The basic function is this, is created by artificial intelligence and not works very well.
        # Generate star polygon vertices
        points_cw = []
        points_ccw = []
        for i in range(steps):
            t = 2 * math.pi * i / steps
            r = outer_radius if i % 2 == 0 else inner_radius
            x_cw = center[0] + r * math.cos(t)
            y_cw = center[1] + r * math.sin(t)
            x_ccw = center[0] + r * math.cos(-t + math.pi / max(lobes, 1))
            y_ccw = center[1] + r * math.sin(-t + math.pi / max(lobes, 1))
            points_cw.append((x_cw, y_cw))
            points_ccw.append((x_ccw, y_ccw))
See one random example with this tool:

Saturday, November 28, 2015

The svgwrite python module - part 001.

The svgwrite python module is a tool by Manfred Moitzi.
To install this python module use Python 3.4 version.
Then you need to install svgwrite with pip3.4:
C:\Python34\Scripts>pip3.4.exe install svgwrite
Collecting svgwrite
  Downloading svgwrite-1.1.6.tar.gz (109kB)
    100% |################################| 110kB 354kB/s
Also this svgwrite is the only module that needs to be imported and then you can deal with this module.
See this simple example:
import svgwrite
from svgwrite import *
#this will work with svgwrite
svg_doc = svgwrite.Drawing(filename = "test-svgwrite.svg",\
                                size = ("30px", "30px"))\
svg_doc.add(svg_doc.rect(insert = (0, 0),\
                                   size = ("20px", "20px"),\
                                   stroke_width = "1",\
                                   stroke = "green",\
                                   fill = "rgb(255,255,0)"))\
print(svg_doc.tostring())
svg_doc.save()
The result of this python script is this SVG file.

If you use the dir() then you can see more about this python module:
>>> dir(svgwrite)
['AUTHOR_EMAIL', 'AUTHOR_NAME', 'CYEAR', 'Drawing', 'Hz', 'Unit', 'VERSION', '__
builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__p
ackage__', '__path__', '__spec__', 'animate', 'base', 'cm', 'container', 'data',
 'deg', 'drawing', 'elementfactory', 'em', 'etree', 'ex', 'filters', 'grad', 'gr
adients', 'image', 'inch', 'kHz', 'masking', 'mixins', 'mm', 'params', 'path', '
pattern', 'pc', 'percent', 'pt', 'px', 'rad', 'rgb', 'shapes', 'text', 'utils',
'validator2', 'version']
For example you can add some text:
svg_doc.add(svg_document.text("This add new svg text",\
                                   insert = (10, 10)))