I'm an artist trying my hand at some generative art, in the very early learning stages. I have some code that does what's expected on the first iteration, it makes a small svg file consisting of dozens of stacked circles, like tree rings:
import numpy as np
import svgwrite
import os
dwg = svgwrite.Drawing('svgobject',size=('620','620'), profile='full') #position One
n_circles = 3 # will generate n files
max_rad=300 # maxiumu radius, modify with interactive() later
r = 1.0 # starting radius, modify with interactive() later
c=[310,310] # Center point of circles, modify with interactive() later
for i in range(n_circles):
# dwg = svgwrite.Drawing('svgobject',size=('620','620'), profile='full') #position Two
while r < max_rad:
step = np.around(np.random.uniform(2, 6),1) # small positive increase in radius
r += step # increment radius
dwg.add(dwg.circle(c, r,stroke="black", fill='none')) # append 'svgobject'
n = 0
while os.path.exists("SVG_Circles%s.svg" % n):
n += 1
dwg.saveas("SVG_Circles%s.svg" % n,pretty=True)
The problem is, on any subsequent iterations it does one of two things. With dwg = ... at position one I get 3 files that are all the same image, no randomization differing from the first run. At position two, I get the first file just fine, then subsequent files are blank.
I suspect I need to find a way to reinitilize the file-like-object 'svgobject' from svgwrite.Drawing(), but I've put it in more or less every possible location, in every loop, and case one or two is all I get.
So how do I get this to iterate in a way that it will generate the svg file, write it to a unique filename, clear my container/fileobject 'svgobject', and then iterate n_circle times?
Thanks!
I rewrote it, this worked for me:
import numpy as np
import svgwrite
import os
def create_img(file_name):
n_circles = 3 # will generate n files
max_rad=300 # maxiumu radius, modify with interactive() later
r = 1.0 # starting radius, modify with interactive() later
c=[310,310] # Center point of circles, modify with interactive() later
dwg = svgwrite.Drawing(size=('620','620')) #position Two
while r < max_rad:
step = np.around(np.random.uniform(2, 6),1) # small positive increase in radius
r += step # increment radius
dwg.add(dwg.circle(c, r,stroke="black", fill='none')) # append 'svgobject'
dwg.saveas(file_name,pretty=True)
files = []
for z in range(3):
file_name = f"SVG_Circles{z}.svg"
create_img(file_name)