A Python library to create SVG drawings programmatically.
Professional graphics capabilities including gradients, patterns, filters, animations, and masking effects for creating sophisticated SVG graphics.
Advanced color gradient systems for creating smooth color transitions and professional visual effects.
def linearGradient(start=None, end=None, inherit=None, **extra):
"""
Create linear color gradient between two points
Args:
start: Starting point as (x1, y1) tuple (default: None)
end: Ending point as (x2, y2) tuple (default: None)
inherit: Inherit from another gradient reference (default: None)
**extra: Additional SVG attributes (gradientUnits, spreadMethod, etc.)
Returns:
LinearGradient: Linear gradient element with gradient methods
"""
def radialGradient(center=None, r=None, focal=None, inherit=None, **extra):
"""
Create radial color gradient from center point
Args:
center: Center point as (cx, cy) tuple (default: None)
r: Gradient radius (default: None)
focal: Focal point as (fx, fy) tuple (default: None)
inherit: Inherit from another gradient reference (default: None)
**extra: Additional SVG attributes (gradientUnits, spreadMethod, etc.)
Returns:
RadialGradient: Radial gradient element with gradient methods
"""
# Gradient methods (available on both linear and radial gradients)
def add_stop_color(offset=None, color=None, opacity=None):
"""
Add color stop to gradient
Args:
offset: Position along gradient (0.0 to 1.0 or percentage)
color: Color value at this stop
opacity: Color opacity at this stop (default: None)
"""
def add_colors(colors, sweep=(0., 1.), opacity=None):
"""
Add multiple colors with automatic linear distribution
Args:
colors: List of color values
sweep: Range tuple (start, end) for color distribution (default: (0., 1.))
opacity: Opacity value or list of opacities (default: None)
"""
def get_paint_server(default='none'):
"""
Get functional IRI reference for use in fill or stroke
Args:
default: Fallback value if gradient not available (default: 'none')
Returns:
str: FuncIRI reference like 'url(#gradient-id)'
"""Usage Examples:
import svgwrite
dwg = svgwrite.Drawing('gradients.svg', size=('400px', '300px'))
# Create definitions container
defs = dwg.defs()
# Linear gradient from red to blue
linear_grad = dwg.linearGradient(start=(0, 0), end=(100, 0), id='linear1')
linear_grad.add_stop_color(0, 'red')
linear_grad.add_stop_color(0.5, 'yellow')
linear_grad.add_stop_color(1, 'blue')
defs.add(linear_grad)
# Radial gradient with opacity
radial_grad = dwg.radialGradient(center=(50, 50), r=40, id='radial1')
radial_grad.add_stop_color(0, 'white', opacity=1.0)
radial_grad.add_stop_color(1, 'black', opacity=0.3)
defs.add(radial_grad)
# Multi-color gradient using add_colors
rainbow_grad = dwg.linearGradient(start=(0, 0), end=(200, 0), id='rainbow')
rainbow_colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
rainbow_grad.add_colors(rainbow_colors)
defs.add(rainbow_grad)
dwg.add(defs)
# Apply gradients to shapes
linear_rect = dwg.rect(insert=(20, 20), size=(150, 60),
fill=linear_grad.get_paint_server())
dwg.add(linear_rect)
radial_circle = dwg.circle(center=(300, 70), r=50,
fill=radial_grad.get_paint_server())
dwg.add(radial_circle)
rainbow_rect = dwg.rect(insert=(50, 150), size=(300, 40),
fill=rainbow_grad.get_paint_server())
dwg.add(rainbow_rect)
# Gradient with custom spread method
spread_grad = dwg.linearGradient(start=(0, 0), end=(50, 0), id='spread',
spreadMethod='reflect')
spread_grad.add_stop_color(0, 'purple')
spread_grad.add_stop_color(1, 'cyan')
defs.add(spread_grad)
spread_rect = dwg.rect(insert=(50, 220), size=(200, 50),
fill=spread_grad.get_paint_server())
dwg.add(spread_rect)Repeating pattern definitions for complex fill and stroke effects.
def pattern(insert=None, size=None, inherit=None, **extra):
"""
Create repeating pattern for fill and stroke
Args:
insert: Pattern origin as (x, y) tuple (default: None)
size: Pattern tile size as (width, height) tuple (default: None)
inherit: Inherit from another pattern reference (default: None)
**extra: Additional SVG attributes (patternUnits, patternTransform, etc.)
Returns:
Pattern: Pattern element with ViewBox, Transform, and Presentation mixins
"""
def get_paint_server(default='none'):
"""
Get functional IRI reference for use in fill or stroke
Args:
default: Fallback value if pattern not available (default: 'none')
Returns:
str: FuncIRI reference like 'url(#pattern-id)'
"""Usage Examples:
import svgwrite
dwg = svgwrite.Drawing('patterns.svg', size=('400px', '300px'))
defs = dwg.defs()
# Checkerboard pattern
checker_pattern = dwg.pattern(insert=(0, 0), size=(20, 20), id='checkerboard')
checker_pattern.add(dwg.rect((0, 0), (10, 10), fill='black'))
checker_pattern.add(dwg.rect((10, 10), (10, 10), fill='black'))
checker_pattern.add(dwg.rect((0, 10), (10, 10), fill='white'))
checker_pattern.add(dwg.rect((10, 0), (10, 10), fill='white'))
defs.add(checker_pattern)
# Dot pattern
dot_pattern = dwg.pattern(insert=(0, 0), size=(15, 15), id='dots')
dot_pattern.add(dwg.circle((7.5, 7.5), 3, fill='red'))
defs.add(dot_pattern)
# Stripe pattern with gradient
stripe_pattern = dwg.pattern(insert=(0, 0), size=(10, 10), id='stripes')
stripe_gradient = dwg.linearGradient((0, 0), (10, 0), id='stripe-grad')
stripe_gradient.add_stop_color(0, 'blue')
stripe_gradient.add_stop_color(1, 'lightblue')
defs.add(stripe_gradient)
stripe_pattern.add(dwg.rect((0, 0), (5, 10), fill=stripe_gradient.get_paint_server()))
stripe_pattern.add(dwg.rect((5, 0), (5, 10), fill='white'))
defs.add(stripe_pattern)
dwg.add(defs)
# Apply patterns to shapes
checker_rect = dwg.rect(insert=(20, 20), size=(120, 80),
fill=checker_pattern.get_paint_server(),
stroke='black', stroke_width=2)
dwg.add(checker_rect)
dot_circle = dwg.circle(center=(250, 60), r=40,
fill=dot_pattern.get_paint_server(),
stroke='darkred', stroke_width=2)
dwg.add(dot_circle)
stripe_ellipse = dwg.ellipse(center=(200, 180), r=(80, 40),
fill=stripe_pattern.get_paint_server())
dwg.add(stripe_ellipse)SVG animation system for creating dynamic and interactive graphics.
def animate(attributeName=None, values=None, href=None, **extra):
"""
Create attribute interpolation animation
Args:
attributeName: SVG attribute to animate
values: Semicolon-separated list of values (default: None)
href: Target element reference (default: None)
**extra: Animation timing attributes (dur, begin, end, etc.)
Returns:
Animate: Animation element with timing control methods
"""
def set(href=None, **extra):
"""
Create simple attribute value setting animation
Args:
href: Target element reference (default: None)
**extra: Animation and timing attributes
Returns:
Set: Set animation element with timing methods
"""
def animateColor(attributeName=None, values=None, href=None, **extra):
"""
Create color transformation animation
Args:
attributeName: Color attribute to animate (fill, stroke, etc.)
values: Semicolon-separated color values (default: None)
href: Target element reference (default: None)
**extra: Animation timing attributes
Returns:
AnimateColor: Color animation element
"""
def animateTransform(transform, element=None, **extra):
"""
Create transform attribute animation
Args:
transform: Transform type ('translate', 'scale', 'rotate', 'skewX', 'skewY')
element: Target element reference (default: None)
**extra: Transform values and timing attributes
Returns:
AnimateTransform: Transform animation element
"""
def animateMotion(path=None, href=None, **extra):
"""
Create motion animation along a path
Args:
path: SVG path data for motion (default: None)
href: Target element reference (default: None)
**extra: Motion and timing attributes (dur, rotate, etc.)
Returns:
AnimateMotion: Motion animation element
"""
# Animation timing methods (available on all animation elements)
def set_timing(begin=None, end=None, dur=None, min=None, max=None,
restart=None, repeatCount=None, repeatDur=None):
"""Set animation timing parameters"""
def set_target(attributeName, attributeType=None):
"""Set animation target attribute"""
def set_value(values, calcMode=None, keyTimes=None, keySplines=None,
from_=None, to=None, by=None):
"""Set animation values and interpolation"""Usage Examples:
import svgwrite
dwg = svgwrite.Drawing('animations.svg', size=('400px', '300px'))
# Animated circle with changing radius
circle = dwg.circle(center=(100, 100), r=20, fill='red', id='animated-circle')
radius_anim = dwg.animate(attributeName='r', values='20;40;20', dur='2s',
repeatCount='indefinite')
circle.add(radius_anim)
dwg.add(circle)
# Color animation
color_rect = dwg.rect(insert=(200, 50), size=(80, 60), fill='blue')
color_anim = dwg.animateColor(attributeName='fill',
values='blue;red;green;blue',
dur='3s', repeatCount='indefinite')
color_rect.add(color_anim)
dwg.add(color_rect)
# Transform animation - rotation
rotating_rect = dwg.rect(insert=(320, 80), size=(30, 30), fill='purple')
rotate_anim = dwg.animateTransform(
transform='rotate',
values='0 335 95;360 335 95', # Rotate around center
dur='2s',
repeatCount='indefinite'
)
rotating_rect.add(rotate_anim)
dwg.add(rotating_rect)
# Motion along path
motion_circle = dwg.circle(center=(0, 0), r=8, fill='orange')
motion_anim = dwg.animateMotion(
path='M 50,200 Q 200,150 350,200 T 350,250',
dur='4s',
repeatCount='indefinite',
rotate='auto' # Auto-orient along path
)
motion_circle.add(motion_anim)
dwg.add(motion_circle)
# Show the motion path for reference
motion_path = dwg.path(d='M 50,200 Q 200,150 350,200 T 350,250',
stroke='lightgray', stroke_width=1, fill='none')
dwg.add(motion_path)
# Complex animation with multiple attributes
complex_rect = dwg.rect(insert=(50, 250), size=(20, 20), fill='green')
# Animate position
pos_anim = dwg.animateTransform(
transform='translate',
values='0,0;100,0;100,-50;0,-50;0,0',
dur='5s',
repeatCount='indefinite'
)
# Animate size
size_anim = dwg.animate(attributeName='width', values='20;40;20',
dur='2.5s', repeatCount='indefinite')
complex_rect.add(pos_anim)
complex_rect.add(size_anim)
dwg.add(complex_rect)Advanced filter system for creating visual effects like blurs, lighting, and compositing.
def filter(start=None, size=None, resolution=None, inherit=None, **extra):
"""
Create filter container for visual effects
Args:
start: Filter region start as (x, y) tuple (default: None)
size: Filter region size as (width, height) tuple (default: None)
resolution: Filter resolution (default: None)
inherit: Inherit from another filter (default: None)
**extra: Additional filter attributes (filterUnits, primitiveUnits)
Returns:
Filter: Filter container element for adding filter primitives
"""
# Filter primitives (accessed via Filter instance dynamic attributes)
def feGaussianBlur(**kwargs):
"""Gaussian blur effect"""
def feOffset(**kwargs):
"""Offset/displacement effect"""
def feColorMatrix(**kwargs):
"""Color transformation matrix"""
def feFlood(**kwargs):
"""Solid color flood"""
def feBlend(**kwargs):
"""Blending of two inputs"""
def feComposite(**kwargs):
"""Compositing operations"""
def feMorphology(**kwargs):
"""Morphological operations (dilate, erode)"""
def feConvolveMatrix(**kwargs):
"""Convolution matrix filter"""
def feTurbulence(**kwargs):
"""Procedural noise generation"""Usage Examples:
import svgwrite
dwg = svgwrite.Drawing('filters.svg', size=('400px', '300px'))
defs = dwg.defs()
# Drop shadow filter
shadow_filter = dwg.filter(id='drop-shadow')
shadow_filter.feOffset(in_='SourceGraphic', dx=3, dy=3, result='offset')
shadow_filter.feGaussianBlur(in_='offset', stdDeviation=2, result='blur')
shadow_filter.feFlood(flood_color='black', flood_opacity=0.3, result='flood')
shadow_filter.feComposite(in_='flood', in2='blur', operator='in', result='shadow')
shadow_filter.feMerge(
feMergeNode(in_='shadow'),
feMergeNode(in_='SourceGraphic')
)
defs.add(shadow_filter)
# Glow filter
glow_filter = dwg.filter(id='glow')
glow_filter.feGaussianBlur(stdDeviation=4, result='coloredBlur')
glow_filter.feMerge(
feMergeNode(in_='coloredBlur'),
feMergeNode(in_='SourceGraphic')
)
defs.add(glow_filter)
# Emboss filter
emboss_filter = dwg.filter(id='emboss')
emboss_filter.feConvolveMatrix(
kernelMatrix='-2 -1 0 -1 1 1 0 1 2',
result='emboss'
)
defs.add(emboss_filter)
dwg.add(defs)
# Apply filters to elements
shadow_rect = dwg.rect(insert=(50, 50), size=(100, 60), fill='lightblue',
filter='url(#drop-shadow)')
dwg.add(shadow_rect)
glow_text = dwg.text('Glowing Text', insert=(200, 80), font_size='20px',
fill='yellow', filter='url(#glow)')
dwg.add(glow_text)
emboss_circle = dwg.circle(center=(300, 200), r=40, fill='lightgreen',
filter='url(#emboss)')
dwg.add(emboss_circle)Advanced masking and clipping capabilities for complex visibility control.
def mask(start=None, size=None, **extra):
"""
Create alpha mask for compositing operations
Args:
start: Mask region start as (x, y) tuple (default: None)
size: Mask region size as (width, height) tuple (default: None)
**extra: Additional mask attributes (maskUnits, maskContentUnits)
Returns:
Mask: Mask definition element
"""
def clipPath(**extra):
"""
Create clipping path for restricting paint regions
Args:
**extra: Additional clipPath attributes (clipPathUnits, etc.)
Returns:
ClipPath: Clipping path element with Transform mixin
"""Usage Examples:
import svgwrite
dwg = svgwrite.Drawing('masking.svg', size=('400px', '300px'))
defs = dwg.defs()
# Create circular clipping path
circle_clip = dwg.clipPath(id='circle-clip')
circle_clip.add(dwg.circle(center=(100, 100), r=50))
defs.add(circle_clip)
# Create gradient mask
gradient_mask = dwg.mask(id='fade-mask')
mask_gradient = dwg.linearGradient(start=(0, 0), end=(100, 0), id='mask-grad')
mask_gradient.add_stop_color(0, 'white')
mask_gradient.add_stop_color(1, 'black')
defs.add(mask_gradient)
mask_rect = dwg.rect(insert=(0, 0), size=(200, 100),
fill=mask_gradient.get_paint_server())
gradient_mask.add(mask_rect)
defs.add(gradient_mask)
# Text mask
text_mask = dwg.mask(id='text-mask')
text_mask.add(dwg.text('MASK', insert=(150, 200), font_size='48px',
font_weight='bold', fill='white'))
defs.add(text_mask)
dwg.add(defs)
# Apply clipping path
clipped_image = dwg.rect(insert=(50, 50), size=(100, 100), fill='red',
clip_path='url(#circle-clip)')
dwg.add(clipped_image)
# Apply gradient mask
masked_rect = dwg.rect(insert=(200, 50), size=(150, 100), fill='blue',
mask='url(#fade-mask)')
dwg.add(masked_rect)
# Apply text mask to pattern
pattern = dwg.pattern(insert=(0, 0), size=(20, 20), id='bg-pattern')
pattern.add(dwg.rect((0, 0), (20, 20), fill='yellow'))
pattern.add(dwg.circle((10, 10), 8, fill='red'))
defs.add(pattern)
masked_background = dwg.rect(insert=(100, 160), size=(200, 80),
fill=pattern.get_paint_server(),
mask='url(#text-mask)')
dwg.add(masked_background)Solid color paint server for SVG 1.2 Tiny profile.
def solidColor(color="currentColor", opacity=None, **extra):
"""
Create solid color paint server for SVG 1.2 Tiny profile
Args:
color: Color value (default: "currentColor")
opacity: Color opacity (default: None)
**extra: Additional SVG attributes
Returns:
SolidColor: Solid color element with get_paint_server method
"""Usage Examples:
import svgwrite
# Note: SolidColor only works with SVG 1.2 Tiny profile
dwg = svgwrite.Drawing('solidcolor.svg', profile='tiny', size=('300px', '200px'))
defs = dwg.defs()
# Solid color definitions
red_color = dwg.solidColor(color='red', opacity=0.7, id='semi-red')
blue_color = dwg.solidColor(color='blue', id='solid-blue')
defs.add(red_color)
defs.add(blue_color)
dwg.add(defs)
# Use solid colors
red_rect = dwg.rect(insert=(50, 50), size=(80, 60),
fill=red_color.get_paint_server())
dwg.add(red_rect)
blue_circle = dwg.circle(center=(200, 100), r=40,
fill=blue_color.get_paint_server())
dwg.add(blue_circle)Install with Tessl CLI
npx tessl i tessl/pypi-svgwrite