如何将乌龟输出作为图像保存?
我有一个代码并绘制圆圈。我想将输出作为图像保存。我能够将输出作为.svg文件保存。但是当我尝试打开时,它只显示白页。我还尝试将其转到.jpg或.jpeg版本。再次,我只看到白屏。我该如何解决问题?
from turtle import Screen, Turtle
from random import randint
from svg_turtle import SvgTurtle
I have a code and draw circles. I would like to save the output as image. I am able to save the output as .svg file. But when i try to open, it only shows white page. I also tried to turn it to .jpg or .jpeg version. Again I see only the white screen. How can I solve the problem?
from turtle import Screen, Turtle
from random import randint
from svg_turtle import SvgTurtle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个很好的例子,说明为什么最小的例子是调试中的critcal。删除了所有无关的绘图代码后,您将剩下:
您遇到了很多麻烦,可以创建
svgturtle
fiber
仅将其覆盖普通的旧turtle()
实例在您绘制任何内容之前。删除
fiber = turtle()
并使用svgturtle
fiber
而不是。这会产生新的问题:
可以通过用
屏幕
乌龟镜像所有光纤
命令来解决问题1,甚至对您的用例也可能无关紧要。如果您不关心屏幕,则可以将其保留,或者暂时提出屏幕以验证您的绘图逻辑,以通过Turtle()
fiber
交换来验证您的图形逻辑在开发过程中。问题2似乎是SVG库中的一个特征,即它不会为SVG编写Sprite位置。
邮票
而不是showturtle
工作。这是一个新版本,通过一些简化实现这些更改(
MAP
过于复杂):This is a good example of why a minimal example is critcal in debugging. Once you remove all of the irrelevant drawing code, you're left with:
You've gone through a lot of trouble to create a
SvgTurtle
fiber
only to overwrite it with a plain oldTurtle()
instance before you draw anything with it.Remove
fiber = Turtle()
and use theSvgTurtle
fiber
instead.This creates new problems:
Problem 1 can be solved by mirroring all
fiber
commands with aScreen
turtle and may not even matter for your use case. If you don't care about the screen, you can keep it off, or temporarily bring it up to validate your drawing logic by swapping in aTurtle()
for thefiber
during development.Problem 2 seems to be a characteristic in the SVG library which is that it doesn't write sprite positions to SVG.
stamp
rather thanshowturtle
works.Here's a new version that implements these changes with a few simplifications (
map
was overly complex):