mdaneels
mdaneels

Reputation: 13

Saving turtle drawing in python

I want to save a turtle drawing to and .eps file. Right now I use the function screen.getcanvas().postscript(file="test.eps")

The problem with that is it doesn't save the complete drawing. Before I draw, I don't know how big the drawing will be. When I drew, I have the biggest x and y value. The function like above just saves the drawing, what is on the canvas. If the drawing is really big, it is not completely on the canvas but I want to save the complete drawing. How do I do that?

Upvotes: 1

Views: 868

Answers (1)

cdlane
cdlane

Reputation: 41905

The key to this is to setup() the visible drawing, and use screensize() to allocate the size of your image's total backing store. And then use the x, y, width, and height arguments of the postscript() method to capture what you want.

The following will draw a visible circle and one too large to be seen in the window. It will then dump the entire image with both circles:

from turtle import Screen, Turtle

screen = Screen()
screen.setup(600, 480)  # what's visible
screen.screensize(1200, 960)  # total backing store

turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')

turtle.penup()
turtle.sety(-100)
turtle.pendown()
turtle.circle(100)  # visible

turtle.penup()
turtle.sety(-400)
turtle.pendown()
turtle.circle(400)  # offscreen

canvas = screen.getcanvas()

canvas.postscript(file="test.eps", x=-600, y=-480, width=1200, height=960)

# wait for program to quit, then examine file 'test.eps'

Although you'll need to preallocate the large backing store (screensize()), you only need to dump as much of it as you want by keeping track of your user's actions.

Upvotes: 1

Related Questions