我正在用 Python 制作一个抽象艺术模板生成器,它接受最小半径、最大半径和圆数的输入。它在随机的地方绘制随机的圆圈,也满足用户的要求。我想将海龟图形转换为 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()
您可以使用
turtle.getcanvas()
生成 Tkinker 画布。然后将其另存为 postscript 文件。
...
cv = turtle.getcanvas()
cv.postscript(file="file_name.ps", colormode='color')
turtle.done()
然后你可以将它转换为png(我想你会找到如何做的)。或者将 PIL 与 Tkinker 结合使用 - 有关此方法的更多信息此处
可以使用 Ghostscript 将 postscript 文件 (*.ps) 转换为 PNG。该开源程序可在多个平台上使用。 另一个选择是 ImageMagick,也是开源和多平台的。
只需在互联网上搜索“将 ps 转换为 PNG Ghostscript”或“将 ps 转换为 PNG Imagemagick”即可。
如果您想自动执行转换,请查看
subprocess
模块(Python 文档)以从 Python 程序中调用该程序。