473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Saving output of Turtle Graphics?

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

Apr 7 '07 #1
7 20627
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.
Apr 7 '07 #2
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
Apr 7 '07 #3
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.
Apr 7 '07 #4
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

Apr 7 '07 #5
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.
Apr 7 '07 #6
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
Apr 7 '07 #7
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
Apr 7 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dbrown2 | last post by:
I typically use IDLE for editing and debug. On Windows at least, IDLE and the standard turtle graphics module do not mix. I think both use Tkinter. For now I use IPython and Jedit when using...
1
by: Elaine Jackson | last post by:
Newbie. Playing with the 'turtle' module and wondering if there's a way to save the graphics you make with it. The documentation itself has nothing to say about this, nor (as far as I can tell)...
4
by: Brent W. Hughes | last post by:
If the Turtle gets himself into a large (or infinite) loop, is there any way to stop him other than Ctrl-Alt-Del? Brent
2
by: jevitop | last post by:
Hi, im looking for help on how to make a turtle graphics program. My head will explode trying to figure this out. if anyone can help me please do it, i'll appriciate it!!! Can anyone...
5
by: Vin | last post by:
Hi, I am using the following code to draw whatever the user draws using x,y. // draws lines directly on a winform. CreateGraphics().DrawLine(APen, x, y, OldX, OldY); Now how do I save the...
2
by: Mark Denardo | last post by:
Hi, I need some expert GDI+ person to help me with my RoundOffImage Function: What I'm trying to do is take in an image, crop off the edges around an ellipse region I set up, and then return the...
5
by: tomy | last post by:
Hi All I am a newbie to turtle graphics in python, so sorry if you find this question too easy. How can I get smoother lines in turtle graphics? I am using python on windows. Thanks in advance
4
by: hello123 | last post by:
I saw this program from a website... Any solution ? Turtle Graphics: The Logo language, which is particularly popular among personal computer user, made the concept of turtle graphics famous. ...
1
by: Alexander.Oot | last post by:
I think the speed function may be broken from the turtle graphics package "from turtle import * speed('fastest') forward(50)"
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.