Python Turtle 模块 - 保存图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4071633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:06:56  来源:igfitidea点击:

Python Turtle Module- Saving an image

pythonvector-graphicsturtle-graphics

提问by jjclarkson

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?

我想弄清楚如何在使用python的turtle模块创建绘图后保存位图或矢量图形图像。经过一番谷歌搜索后,我找不到一个简单的答案。我确实找到了一个名为canvas2svg的模块,但我对 python 很陌生,我不知道如何安装该模块。是否有一些内置的方法来保存海龟画布的图像?如果不是,我应该将 python 的自定义模块放在 Ubuntu 机器上的什么位置?

采纳答案by KilyenOrs

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.

canvas(tkinter)对象具有postscript功能;你可以使用它。

The turtle module has "getscreen" which gives you the "turtle screen" which gives you the Tiknter canvas in which the turtle is drawing.

海龟模块具有“getscreen”,它为您提供“海龟屏幕”,它为您提供海龟正在其中绘制的 Tiknter 画布。

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.

这会将您保存为封装的 PostScript 格式,因此您肯定可以在 GIMP 中使用它,但也有其他查看器。或者,您可以谷歌如何从中制作 .gif。

回答by Don Kirkby

I wrote an SvgTurtleclass that supports the standard Turtle interface from Python, and writes an SVG file using the svgwritemodule. Install svgwrite, download svg_turtle.py, and then call it like this:

我编写了一个SvgTurtle支持 Python 标准 Turtle 接口的类,并使用svgwrite模块编写了一个 SVG 文件。安装 svgwrite,下载svg_turtle.py,然后像这样调用它:

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