Velocity Reviews > Saving output of Turtle Graphics?

# Saving output of Turtle Graphics?

Dick Moores
Guest
Posts: n/a

 04-07-2007
I accidentally stumbled across the Turtle Graphics module (turtle.py)
the other day and have been having some fun with it.

Now I'm wondering if there is a way to build into a script the saving
of each window just before it is cleared. For example, here are a
couple that I've saved by screen capture:
<http://www.rcblue.com/Misc/RandomTriangles.jpg>
<http://www.rcblue.com/Misc/RandomTriangles2.jpg>

They were produced by this script:

================================================== =======
# randomTriangles.py

import turtle as T
from random import *

def twoRndN(low=0, high=1):
"""
generate two random floats x, y in the range [low, high) such that x <= y
"""
x, y = uniform(low, high), uniform(low, high)
if x <= y:
return x, y
else:
return y, x

T.setup(width=1000, height=700, startx=0, starty=0)
T.title("Random Triangles with random R,G,B")

colorRange = "all"
if colorRange == "random":
lowR, highR = twoRndN()
lowG, highG = twoRndN()
lowB, highB = twoRndN()

count = 0
for n in range(300):
wdth = randrange(0,7,3)
T.width(wdth)
T.speed("fastest")
if colorRange == "dark":
R = uniform(.1, .5)
G = uniform(.1, .5)
B = uniform(.1, .5)
elif colorRange == "pastel":
R = uniform(.5, .9)
G = uniform(.5, .9)
B = uniform(.5, .9)
elif colorRange == "all":
R = uniform(0, 1)
G = uniform(0, 1)
B = uniform(0, 1)
# set RGB for one color of your choice
elif colorRange == "manual":
R = .45
G = .2
B = .2
elif colorRange == "random":
R = uniform(lowR, highR)
G = uniform(lowG, highG)
B = uniform(lowB, highB)

T.color(R,G,B)
T.begin_fill()
# 2 connected lines will fill as a triangle
for x in range(2):
coord = (randint(-500,500), randint(-350,350))
T.goto(coord)
T.end_fill()

count += 1
if count > 5:
clr = randint(0,5)
if clr == 0:
T.clear()
count = 0
T.done()
==============================================
(The docs for Turtle graphics for Tk are at
<http://www.python.org/doc/2.5/lib/module-turtle.html>)

But how could I have saved them "automatically"?

The script as shown will clear (T.clear() -- the 3rd line from the
bottom) the window after producing 6 to maybe 15 superimposed
triangles, so clearing will take place maybe 30 times. How can I save
as images each of the 30 windows just before they are cleared?

Thanks,

Dick Moores

=?ISO-8859-2?Q?Wojciech_Mu=B3a?=
Guest
Posts: n/a

 04-07-2007
Dick Moores wrote:
> I accidentally stumbled across the Turtle Graphics module (turtle.py)
> the other day and have been having some fun with it.
>
> Now I'm wondering if there is a way to build into a script the saving of
> each window just before it is cleared. For example, here are a couple
> that I've saved by screen capture:
> <http://www.rcblue.com/Misc/RandomTriangles.jpg>
> <http://www.rcblue.com/Misc/RandomTriangles2.jpg>

Turtle module uses Tk canvas element to draw graphics ('_canvas'
attribute). I've written module, that exports canvas graphics to SVG
file: http://wmula.republika.pl/proj/canvas2svg/ -- it may be useful
for you.

w.

Dick Moores
Guest
Posts: n/a

 04-07-2007
At 06:50 AM 4/7/2007, =?ISO-8859-2?Q?Wojciech_Mu=B3a?= wrote:
>Dick Moores wrote:
> > I accidentally stumbled across the Turtle Graphics module (turtle.py)
> > the other day and have been having some fun with it.
> >
> > Now I'm wondering if there is a way to build into a script the saving of
> > each window just before it is cleared. For example, here are a couple
> > that I've saved by screen capture:
> > <http://www.rcblue.com/Misc/RandomTriangles.jpg>
> > <http://www.rcblue.com/Misc/RandomTriangles2.jpg>

>
>Turtle module uses Tk canvas element to draw graphics ('_canvas'
>attribute). I've written module, that exports canvas graphics to SVG
>file: http://wmula.republika.pl/proj/canvas2svg/ -- it may be useful
>for you.

I afraid I'm totally unfamiliar with SVG. Would it be possible for
you or someone else on the list to show how to use your module to
export the simple product of this simple script to an SVG file?

===============================================
import turtle as T
from random import randint
T.setup(width=1000, height=700, startx=0, starty=0)
T.color(1, .5, .5)
T.begin_fill()
# 2 connected lines will fill as a triangle
for x in range(2):
coord = (randint(-500,500), randint(-350,350))
T.goto(coord)
T.end_fill()

T.done()
================================================

Thanks,

Dick Moores
Win XP Pro SP2
Python 2.5
Python IDE: Ulipad 3.6

=?ISO-8859-2?Q?Wojciech_Mu=B3a?=
Guest
Posts: n/a

 04-07-2007
Dick Moores wrote:
>> Turtle module uses Tk canvas element to draw graphics ('_canvas'
>> attribute). I've written module, that exports canvas graphics to SVG
>> file: http://wmula.republika.pl/proj/canvas2svg/ -- it may be useful
>> for you.

>
> I afraid I'm totally unfamiliar with SVG. Would it be possible for you
> or someone else on the list to show how to use your module to export the
> simple product of this simple script to an SVG file?
>
> ===============================================
> import turtle as T

import canvasvg
> from random import randint
> T.setup(width=1000, height=700, startx=0, starty=0)
> T.color(1, .5, .5)
> T.begin_fill()
> # 2 connected lines will fill as a triangle
> for x in range(2):
> coord = (randint(-500,500), randint(-350,350))
> T.goto(coord)
> T.end_fill()

canvasvg.saveall("image.svg", T._canvas)

> T.done()
> ================================================

w.

Dick Moores
Guest
Posts: n/a

 04-07-2007
At 08:48 AM 4/7/2007, =?ISO-8859-2?Q?Wojciech_Mu=B3a?= wrote:
>Dick Moores wrote:
> >> Turtle module uses Tk canvas element to draw graphics ('_canvas'
> >> attribute). I've written module, that exports canvas graphics to SVG
> >> file: http://wmula.republika.pl/proj/canvas2svg/ -- it may be useful
> >> for you.

> >
> > I afraid I'm totally unfamiliar with SVG. Would it be possible for you
> > or someone else on the list to show how to use your module to export the
> > simple product of this simple script to an SVG file?
> >
> > ===============================================
> > import turtle as T

>import canvasvg
> > from random import randint
> > T.setup(width=1000, height=700, startx=0, starty=0)
> > T.color(1, .5, .5)
> > T.begin_fill()
> > # 2 connected lines will fill as a triangle
> > for x in range(2):
> > coord = (randint(-500,500), randint(-350,350))
> > T.goto(coord)
> > T.end_fill()

>
>canvasvg.saveall("image.svg", T._canvas)
>
> > T.done()
> > ================================================

OK, thanks, now I've got

================================================== =========
<?xml version="1.0" ?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG
1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg
height="246.000" viewBox="8.000 338.000 504.000 246.000"
width="504.000" xmlns="http://www.w3.org/2000/svg"><line fill="none"
stroke="#ff8080" stroke-linecap="round" x1="500.0" x2="20.0"
y1="350.0" y2="426.0"/><line fill="none" stroke="#ff8080"
stroke-linecap="round" x1="20.0" x2="368.0" y1="426.0"
y2="569.0"/><line fill="none" stroke="#ff8080" stroke-linecap="round"
x1="360.0" x2="368.0" y1="569.0" y2="569.0"/><polygon fill="#ff8080"
points="368.0 569.0 358.0 572.0 360.0 569.0 358.0 566.0"/><polygon
fill="#ff8080" fill-rule="evenodd" points="500.0,350.0 20.0,426.0
368.0,569.0" stroke-linejoin="round"/></svg>
================================================== ==========

What do I do to see this?

Dick

=?ISO-8859-2?Q?Wojciech_Mu=B3a?=
Guest
Posts: n/a

 04-07-2007
Dick Moores wrote:
> What do I do to see this?

For example Opera 9 and Firefox 1.5+ are able to view SVG files;
there is a free plugin for IrfanView.

w.

Peter Otten
Guest
Posts: n/a

 04-07-2007
Dick Moores wrote:

> OK, thanks, now I've got

[an svg file]

> What do I do to see this?

You can convert it to a jpeg using ImageMagick's convert.

Peter

Dick Moores
Guest
Posts: n/a

 04-07-2007
At 09:31 AM 4/7/2007, =?ISO-8859-2?Q?Wojciech_Mu=B3a?= wrote:
>Dick Moores wrote:
> > What do I do to see this?

>
>For example Opera 9 and Firefox 1.5+ are able to view SVG files;
>there is a free plugin for IrfanView.

Ha. I had tried it with Firefox 2 already, but I stupidly changed the
extension to HTM first. I'll also get the IrfanView plugin.

Thanks for all the help, and especially for your canvasvg.py module.

Dick