I would like to figure out how to save a bitmap or vector graphics image after creating a drawing with python's turtle module. After a bit of googling I can't find an easy answer. I did find a module called canvas2svg, but I'm very new to python and I don't know how to install the module. Is there some built in way to save images of the turtle canvas? If not where do I put custom modules for python on an Ubuntu machine?
5 Answers
from Tkinter import *
from turtle import *
import turtle
forward(100)
ts = turtle.getscreen()
ts.getcanvas().postscript(file="duck.eps")
This will help you; I had the same problem, I Googled it, but solved it by reading the source of the turtle module.
The canvas (tkinter) object has the postscript function; you can use it.
The turtle module has "getscreen" which gives you the "turtle screen" which gives you the Tiknter canvas in which the turtle is drawing.
This will save you in encapsulated PostScript format, so you can use it in GIMP for sure but there are other viewers too. Or, you can Google how to make a .gif from this.
I wrote an SvgTurtle class that supports the standard Turtle interface from Python, and writes an SVG file using the svgwrite module. Install svgwrite, download svg_turtle.py, and then call it like this:
from turtle import * # @UnusedWildImport
import svgwrite
from svg_turtle import SvgTurtle
def draw_spiral():
fillcolor('blue')
begin_fill()
for i in range(20):
d = 50 + i*i*1.5
pencolor(0, 0.05*i, 0)
width(i)
forward(d)
right(144)
end_fill()
def write_file(draw_func, filename, size):
drawing = svgwrite.Drawing(filename, size=size)
drawing.add(drawing.rect(fill='white', size=("100%", "100%")))
t = SvgTurtle(drawing)
Turtle._screen = t.screen
Turtle._pen = t
draw_func()
drawing.save()
def main():
write_file(draw_spiral, 'example.svg', size=("500px", "500px"))
print('Done.')
if __name__ == '__main__':
main()
It's not working for me. How can I do it so before every screen clear, the output gets saved?
from turtle import *
from random import *
import time
def randomcolour():
colormode(255)
red = randint(0, 255)
green = randint(0, 255)
blue = randint(0, 255)
color(red, green, blue)
def randomplace():
penup()
x = randint(-100, 100)
y = randint(-100, 100)
goto(x, y)
pendown()
def randomheading():
heading = randint(0, 360)
setheading(heading)
def drawrectangle():
randomcolour()
randomplace()
hideturtle()
length = randint(10, 100)
height = randint(10, 100)
begin_fill()
forward(length)
right(90)
forward(height)
right(90)
forward(length)
right(90)
forward(height)
right(90)
end_fill()
shape("turtle")
speed(0)
for i in range(1, 30):
randomcolour()
randomplace()
randomheading()
stamp()
def drawcircle():
radius = randint(5, 100)
randomcolour()
randomplace()
dot(radius)
def drawstar():
randomcolour()
randomplace()
randomheading()
begin_fill()
size = randint(20, 100)
for side in range(5):
left(144)
forward(size)
end_fill()
clear()
setheading(0)
for i in range(20):
drawrectangle()
clear()
for i in range(50):
drawcircle()
clear()
for i in range(20):
sleep(5000)
drawstar()
The way you save a turtle picture is you open python, write the turtle code press Ctrlshifts. Then a window will pop up in the window. Double-click python3.9 then double-click Lib. Keep scrolling down and under the Tkinter module, there will be turtle demo folder. Double-click that and then you can save your turtle files.
If what I said before doesn't work here is a picture step-by-step:
- Hit Ctrl + Shift + S, 2.This image will pop up: https://i.stack.imgur.com/ywqdT.png,
- Click on the lib folder,
- It will bring you here: https://i.stack.imgur.com/QJcH2.png,
- In the search bar at the bottom search: turtledemo,
- It will bring you here: https://i.stack.imgur.com/SZOHu.png,
- In the the text bar at the bottom put in the name of you file and hit enter,
- Then the place you saved it in will close and you can hit Fn + F5,
- It will then run the program(if it doesn't try redoing all the steps)
I am doing this on a windows 10 and I don't know if it works on other kinds computers.