Tim*_*Rin 6 python turtle-graphics
我在Python中制作一个抽象艺术模板生成器,它接受最小半径,最大半径和圆圈数的输入.它在随机位置绘制随机圆圈,也符合用户的规格.我想将Turtle图形转换为PNG,以便用户可以编辑模板但是他/她想要,但我不知道如何继续.这是我的代码:
import random
import time
import turtle
print("Abstract Art Template Generator")
print()
print("This program will generate randomly placed and sized circles on a blank screen.")
num = int(input("Please specify how many circles you would like to be drawn: "))
radiusMin = int(input("Please specify the minimum radius you would like to have: "))
radiusMax = int(input("Please specify the maximum radius you would like to have: "))
screenholder = input("Press ENTER when you are ready to see your circles drawn: ")
t = turtle.Pen()
win = turtle.Screen()
def mycircle():
x = random.randint(radiusMin,radiusMax)
t.circle(x)
t.up()
y = random.randint(0,360)
t.seth(y)
if t.xcor() < -300 or t.xcor() > 300:
t.goto(0, 0)
elif t.ycor() < -300 or t.ycor() > 300:
t.goto(0, 0)
z = random.randint(0,100)
t.forward(z)
t.down()
for i in range(0, num):
mycircle()
turtle.done()
Run Code Online (Sandbox Code Playgroud)
您可以用来turtle.getcanvas()生成 Tkinker 画布。然后将其另存为 postscript 文件。
...
cv = turtle.getcanvas()
cv.postscript(file="file_name.ps", colormode='color')
turtle.done()
Run Code Online (Sandbox Code Playgroud)
然后你可以将它转换为png(我想你会找到如何做的)。或者将 PIL 与 Tkinker 结合使用 - 有关此方法的更多信息请参见此处
可以使用 Ghostscript 将 postscript 文件 (*.ps) 转换为 PNG。该开源程序可在多个平台上使用。另一个选择是 ImageMagick,也是开源和多平台的。
只需在互联网上搜索“将 ps 转换为 PNG Ghostscript”或“将 ps 转换为 PNG Imagemagick”即可。
如果您想自动执行转换,请查看subprocess模块(python 文档)以从 python 程序中调用该程序。
| 归档时间: |
|
| 查看次数: |
4276 次 |
| 最近记录: |