在Python中将乌龟绘制的图像转换为PNG [英] Convert images drawn by turtle to PNG in Python

查看:53
本文介绍了在Python中将乌龟绘制的图像转换为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 制作一个抽象艺术模板生成器,它接受最小半径、最大半径和圆数的输入.它在随机位置绘制随机圆圈,也符合用户的规格.我想将 Turtle 图形转换为 PNG,这样用户就可以按照他/她想要的方式编辑模板,但我不知道如何继续.这是我的代码:

I'm making a abstract art template generator in Python that takes inputs of minimum radius, maximum radius, and number of circles. It draws random circles in random places, also meeting the user's specifications. I want to convert the Turtle graphics into a PNG so that the user can then edit the template however he/she wants to, but I don't know how to proceed. Here's my code:

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

推荐答案

可以使用 ghostscript 将 postscript 文件 (*.ps) 转换为 PNG.此开源程序可在多个平台上使用.另一个选择是 ImageMagick,也是开源和多平台的.

Converting from a postscript file (*.ps) to a PNG can be done with ghostscript. This open-source program is available on multiple platforms. Another option is ImageMagick, also open source and multi platform.

只需在互联网上搜索将 ps 转换为 PNG ghostscript"或将 ps 转换为 PNG Imagemagick".

Just search on the internet for "convert ps to PNG ghostscript" or "convert ps to PNG Imagemagick".

如果您想自动转换,请查看 subprocess 模块(python 文档)以从您的 python 程序中调用该程序.

If you want to automate the conversion, have a look at the subprocess module (python documentation) to call the program from within your python program.

这篇关于在Python中将乌龟绘制的图像转换为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆