pythonturtle-graphicspython-turtle

Save image with Python Turtle Graphics?


I have been trying to create and export an image with python turtle graphics, and I am using turtle.getscreen().getcanvas().postscript(name="foo.esp") to export it (found here). When I call turtle.getscreen(), then the turtle shows up on the screen, and doesn't hide, even when I call the turtle's hideturtle() method.

I tried to delete the turtle using del, but the turtle stayed on screen. I tried calling the turtle's reset method, but that cleared everything it drew. I tried moving it off the screen, but upon calling turtle.getscreen(), it just moved back to the center of the screen.

Here is an example of this:

import turtle
t = turtle.Turtle()

t.fillcolor("red")
t.begin_fill()

t.circle(100)

t.end_fill()
t.getscreen().getcanvas().postscript(file="output.eps")

Solution

  • I figured out how to get the image, but it was kind of complicated. After exporting the image as a .eps file, I opened the file with an editor. Since a .eps file is not binary, I was able to remove the part that draws the turtle, which is located at the bottom of the .eps file, above "grestore" and below "x y z setrgbcolor AdjustColor", where x, y and z are all floats with 3 digits of precision.

    1.000 0.298 0.051 setrgbcolor AdjustColor
    stroke                                    ┐
    grestore                                  │
    gsave                                     │
    grestore                                  │
    gsave                                     │
    grestore                                  │
    gsave                                     │
    grestore                                  │
    gsave                                     │
    0 169 moveto                              │
    -9 174 lineto                             │
    -7 169 lineto                             ├── Delete this part
    -9 164 lineto                             │
    0 169 lineto                              │
    0.000 0.000 0.000 setrgbcolor AdjustColor │
    eofill                                    │
    0 169 moveto                              │
    -9 174 lineto                             │
    -7 169 lineto                             │
    -9 164 lineto                             │
    0 169 lineto                              │
    1 setlinejoin 1 setlinecap                │
    1 setlinewidth                            │
    [] 0 setdash                              │
    0.000 0.000 0.000 setrgbcolor AdjustColor ┘
    stroke
    grestore
    restore showpage
    
    %%Trailer
    end
    %%EOF