34
votes

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
In the event you want to transparently save an image at the end of the program's execution (semi-useful for assisting with grading student assignments that terminate), please check out: github.com/ucsb-cs/cTurtle_screenshot - bboe

5 Answers

44
votes
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.

3
votes

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()
0
votes

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()
0
votes

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.

0
votes

If what I said before doesn't work here is a picture step-by-step:

  1. Hit Ctrl + Shift + S, 2.This image will pop up: https://i.stack.imgur.com/ywqdT.png,
  2. Click on the lib folder,
  3. It will bring you here: https://i.stack.imgur.com/QJcH2.png,
  4. In the search bar at the bottom search: turtledemo,
  5. It will bring you here: https://i.stack.imgur.com/SZOHu.png,
  6. In the the text bar at the bottom put in the name of you file and hit enter,
  7. Then the place you saved it in will close and you can hit Fn + F5,
  8. 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.