在Python中将由Turtle绘制的图像转换为PNG
问题内容:
我正在用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()
问题答案:
从后记文件(*
.ps)转换为PNG可以使用ghostscript完成。此开源程序可在多个平台上使用。另一个选择是ImageMagick,它也是开源和多平台的。
只需在互联网上搜索“将ps转换为PNG ghostscript”或“将ps转换为PNG Imagemagick”即可。
如果要自动执行转换,请查看subprocess
模块(python文档)以从python程序中调用该程序。