python利用Turtle绘图并保存jpg

python2.6版本中后引入的一个简单的绘图工具,叫做海龟绘图(Turtle Graphics)
turtle库是python的内部库,使用导入即可

Turtle是一个非常好用的矢量绘图工具,网上教程很多

import turtle

# 绘图
turtle.tracer(False)
turtle.begin_fill()
turtle.circle(100)
turtle.pencolor("green")
turtle.fillcolor("yellow")
turtle.end_fill()

# 保存
ts = turtle.getscreen()
ts.getcanvas().postscript(file=r"circle.eps")

Turtle保存的格式是eps矢量格式,可以用PS打开,
但通常栅格格式(png、jpg)更通用和方便些

虽然可以用PS直接另存,但比较麻烦,不适合批量操作
所以我们利用PIL另存为jpg格式

from PIL import Image
im = Image.open("circle.eps")
im.save("circle.jpg", "JPEG")

直接用会报错:OSError: Unable to locate Ghostscript on paths
意思是没有在环境变量中找到Ghostscript,这就需要下载安装,并配置环境变量
官网下载地址:https://www.ghostscript.com/download/gsdnld.html
下载可能会比较慢,如果下载Win32位的,可以用我的网盘下载

链接:https://pan.baidu.com/s/12LZryC15hmLRSUH-uGwPyA
提取码:6srb

安装好在path中加入类似

C:\Program Files (x86)\gs\gs9.53.0\bin

重启下IDE或命令行,就可以正常用了(因为修改path后,当前运行环境下需重启才能生效)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章