用画布的postscript方法可以把海龟所画的图形保存为ps格式。这是一种矢量图形模式,它实际上是文本文件,通过一种语言来描述一个图形。本程序把ps格式的图片转换为位图。以下是代码预览:
""" ps2png转换器.py tkinter画布有postscript方法。它的用途是生成ps矢量图形。 这个程序把ps转为png图像。需要用到ghostscript。 """ import os import io from PIL import Image def ps2png(psfile,outfile): """ 转换ps矢量图像到位图 """ f = open(psfile,mode='rb') c = f.read() f.close() im = Image.open(io.BytesIO(c))# 打开图像 im.save(outfile,format="PNG") # 保存图像 im.close() "下面变量需根据安装ghostscript的路径决定" ghostscrpit = 'C:\\Program Files\\gs\\gs9.26\\bin' "以下是添加搜索ghostscript的路径" path = os.environ['path'] path = path.split(";") path.append(ghostscrpit) path = ';'.join(path) os.environ['path'] = path images = [f"images/{i}.ps" for i in range(360)] outims = [f"out/{i}.png" for i in range(360)] [ps2png(infile,outfile) for infile,outfile in zip(images,outims)]
下载完整源代码与素材和ghostscript安装包,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)