STEM

Saving output from Turtle as an image (Python)

Saving output from the turtle module in Python involves a few unique steps compared to other libraries. The turtle module primarily uses the Tkinter canvas to render graphics, and saving these drawings typically requires converting the canvas to a PostScript file using the postscript() method. This file can then be converted to more common image formats like PNG or JPEG using additional libraries such as PIL (Pillow). This process can be somewhat cumbersome as it involves multiple steps and dependencies.

In contrast, other libraries like Matplotlib or Seaborn offer more straightforward methods for saving outputs. For instance, Matplotlib allows you to save plots directly to various formats (e.g., PNG, PDF, SVG) using the savefig() method. Similarly, data output from libraries like Pandas can be saved directly to CSV, Excel, or JSON files using built-in methods like to_csv() or to_excel(). These libraries provide more direct and versatile options for saving outputs, often requiring fewer steps and less additional processing. Yet, in many cases, generating quick graphics and animations are easier in turtle library. Below I’ll share the process by which you can actually save the entire output from your turtle script into a high-quality image file.

Let’s start with actually creating a turtle app where some drawings are done in real-time on a canvas. Our ultimate objective is to save the output, regardless of what it is, from the entire turtle canvas to a file that we can party on. Here’s a little app that generates spiraling squares on the canvas (click on Run to get the output).

This was just a quick example. It could be anything, more complex, larger, or smaller, text, etc. But to save it we can use canvas.postscript(), which produces a PostScript which is a vector format. It’s high quality, and scalable. And the output typically will be in .eps extension. We can open this file with an image viewer that supports the PostScript format, or convert it to another format (like PNG or JPEG) using an image conversion tool.

After your drawing is complete, here’s all the code you need to save the entire canvas as a PostScript file (.eps)…

filename="turtle_image_saved.eps" # specify your path and file name as desired
canvas = getcanvas()
canvas.postscript(file=filename, colormode='color')

This will save the above-mentioned file in the path you specified. You can open it in GIMP or various other software or online webapps that allow opening a PostScript file. For instance, the above spiraling squares output is saved with the above code as a eps file and then converted to a JPG file, which looks like this:

Note however, that the entire window of the canvas will be saved, so it might be much larger than my example above depending on your window size. Typically, the background will be transparent in the output eps file but then you can convert it to PNG to JPG etc. as per your need.

I hope this fun and intriguing. If you would like to get the full source code, please contact me at trseattle at outlook dot com. Readers with confirmed donations will get the source code for free of charge upon request. Thanks for reading!



Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top