Turtle module - Saving an image

39,258

Solution 1

from tkinter import *  # Python 3
#from Tkinter import *  # Python 2
import turtle


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. You can use the free and open source Inkscape application to view .eps files as well, and then save them to vector or bitmap image files.

Solution 2

I wrote the svg-turtle package that supports the standard Turtle interface from Python, and writes an SVG file using the svgwrite module. Install it with pip install svg-turtle, and then call it like this:

from svg_turtle import SvgTurtle


def draw_spiral(t):
    t.fillcolor('blue')
    t.begin_fill()
    for i in range(20):
        d = 50 + i*i*1.5
        t.pencolor(0, 0.05*i, 0)
        t.width(i)
        t.forward(d)
        t.right(144)
    t.end_fill()


def write_file(draw_func, filename, width, height):
    t = SvgTurtle(width, height)
    draw_func(t)
    t.save_as(filename)


def main():
    write_file(draw_spiral, 'example.svg', 500, 500)
    print('Done.')


if __name__ == '__main__':
    main()

The canvasvg package is another option. After you run some turtle code, it will convert all the items on the tkinter canvas into an SVG file. This requires tkinter support and a display, where svg-turtle doesn't.

Share:
39,258
jjclarkson
Author by

jjclarkson

I’m just an average guy with a way above average interest in computer programming, gadgets, and all things hackable. I program for work, I program for fun, and I’m learning more about electronics all the time. I’ll be the first to tell you when I don’t know something, but I might ask a lot of questions. Above all, I’m in it for the thrill of doing something new and exciting. Please feel free to ask me any questions, I love to help.

Updated on January 18, 2022

Comments

  • jjclarkson
    jjclarkson over 2 years

    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?

  • Mohit Pandey
    Mohit Pandey about 7 years
    In turtle of python 2.7, it already has postscript function. So, you don't need to import Tkinter separately.
  • It's Willem
    It's Willem over 6 years
    I'd just like to add that the EPS file gets saved in the same place as the script is.
  • Mr.Weathers
    Mr.Weathers over 6 years
    I believe you should have draw_spiral() --> draw_spiral() In def write_file
  • Don Kirkby
    Don Kirkby over 6 years
    I assume you meant draw_spiral() --> draw_func(), @Mr.Weathers. You're right, that's what I meant to do, so I've fixed it.
  • anotherjesse
    anotherjesse about 4 years
    This works, but you don't need to import Tkiner. But I need to getscreen from the instance of Turtle()
  • diramazioni
    diramazioni over 3 years
    Awesome, problem is that in this way the _Screen object loose a lot attributes because it get overridden by the SvgTurtle screen. You are loose a lot of flexibility to compose the image in several draw pass like clear() or getting the screensize and using it in your function...
  • Don Kirkby
    Don Kirkby over 2 years
    My new version supports a lot more features from the standard Turtle, @diramazioni.