Skip to content

Instantly share code, notes, and snippets.

@Diti Diti/out.svg
Created Feb 1, 2017

Embed
What would you like to do?
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/bin/env python3
'''TODO: Analyze a shooting range target and export a vector (SVG) image out of it.'''
import svgwrite
from svgwrite import cm
def create_target(filename, size):
'''Generate a SVG target and saves it to a file.'''
# Constants
color_dark = 'black'
mid = ('50%', '50%')
#radius_list = [0.25, 0.50, 0.75, 1.00, 1.25, 1.75, 2.00, 2.25]
# Setup `svgwrite`
tgt = svgwrite.Drawing(filename=filename, size=size, profile='tiny')
tgt.viewbox(width=10, height=10)
# Defs
color_dark = tgt.linearGradient(id='color_dark')
color_light = tgt.linearGradient(id='color_light')
color_dark.add_stop_color(color='black')
color_light.add_stop_color(color='rgb(255,240,190)')
tgt.defs.add(color_dark)
tgt.defs.add(color_light)
# Groups
background = tgt.rect(size=size, fill='url(#color_light)')
circles = tgt.g(id='circles', stroke_width='0.0125')
circles_dark = tgt.g(id='circles_dark')
circles_light = tgt.g(id='circles_light')
dark_area = tgt.g(id='dark_area')
# Output elements
tgt.add(background)
tgt.add(dark_area)
tgt.add(circles)
circles.add(circles_light)
circles.add(circles_dark)
# Dark area
dark_area.add(tgt.circle(center=mid, r=1.5, fill='url(#color_dark)'))
# Inside dark area: innermost light circle
circles_light.add(tgt.circle(
center=mid, r=0.0225, fill='url(#color_light)'))
# Inside dark area: all light circles
for cm_increments in enumerate([0.25, 0.50, 0.75, 1.00, 1.25]):
circles_light.add(tgt.circle(center=mid,
r=cm_increments[1],
fill_opacity=0,
stroke='url(#color_light)'))
# Outside dark area: innermost dark circle
circles_dark.add(tgt.circle(center=mid,
r=1.50,
fill_opacity=0,
stroke='url(#color_dark)'))
# Outside dark area: all dark circles
for cm_increments in enumerate([1.75, 2.00, 2.25]):
circles_dark.add(tgt.circle(center=mid,
r=cm_increments[1],
fill_opacity=0,
stroke='url(#color_dark)'))
tgt.save(pretty=True)
if __name__ == '__main__':
create_target('out.svg', (10 * cm, 10 * cm))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.