【发布时间】:2014-08-14 16:02:24
【问题描述】:
我正在编写一个脚本,该脚本将生成一系列使用 python turtle 模块绘制的图像。在对此进行研究时,我发现我可能只能从海龟中获取 EPS 图像,而我已经成功地做到了这一点。为了快速演示,我不得不使用 Photoshop 批量转换图像。因此,我显然想在脚本中进行转换。
- 使用海龟绘制框架
- 在 EPS 中获取框架
- 将帧转换为 JPG
- 重复
脚本中有两个我尝试过的函数,以及 for 循环中的一段简化转换代码。第一个函数产生一条错误消息,第二个函数什么也不做。在 for 循环中的简化转换代码中,我确定了导致问题的行,这可能与 save 方法有关。
我正在使用最新版本的 Pillow 和 Python 3.4 并删除了 PIL。根据下面的回复,我也尝试添加 ghostscript,但这在 Python 3 中不受支持。
#! /usr/local/bin/python3
from turtle import *
from PIL import Image #imports image class
import random
import os, sys
title("Vine Video")
setup(480, 480, 0, 0)
delay(0)
hideturtle()
colormode(255)
def saveImage(fileName):
turtleImage = getscreen()
turtleImage.getcanvas().postscript(file=fileName+".eps")
print("File saved as ",fileName+".eps")
###Functions which haven't worked so far
##def eps2jpg(): ### Convert .eps files to .jpg - function from @trevorappleton
##
## openFiles = glob.glob('*.eps')
## for files in openFiles:
##
## inFile = Image.open(files)
## fileName = os.path.splitext(files)[0] # gets filename
## outFile = fileName + ".jpg"
### Following line produces error
## inFile.save(outFile)
##
## return()
##
##
##
##def eps2jpg2():
## for infile in sys.argv[1:]:
## f, e = os.path.splitext(infile)
## outfile = f + ".jpg"
## if infile != outfile:
## try:
## Image.open(infile).save(outfile)
## except IOError:
## print("cannot convert", infile)
frames = [50,98,141,178,208,228,239]
filenames = ["frame1","frame2","frame3","frame4","frame5","frame6","frame7"]
for i in range(7):
for j in range(150):
randomRed = random.randint(1,254)
randomGreen = random.randint(1,254)
randomBlue = random.randint(1,254)
randomLength = random.randint(0,frames[i])
randomTurn = random.randint(-95,95)
pencolor(randomRed,randomGreen,randomBlue)
forward(randomLength)
right(randomTurn)
goto(0,0)
saveImage(filenames[i])
#simplified conversion code
inFile = Image.open(filenames[i]+".eps")
outFile = filenames[i] + ".jpg"
#following line produces the error: no such file or directory 'gs'
inFile.save(outFile)
clear()
write("done - close turtle window now")
done()
第一个函数和简化转换代码产生的错误信息如下:
Traceback (most recent call last):
File "/Users/sharland/Dropbox/computing_department/Languages and Systems/python/python_scripts/vine_animations/starburst_pulse.py", line 61, in <module>
inFile.save(outFile,"JPEG")
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PIL/Image.py", line 1631, in save
self.load()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PIL/EpsImagePlugin.py", line 361, in load
self.im = Ghostscript(self.tile, self.size, self.fp, scale)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PIL/EpsImagePlugin.py", line 130, in Ghostscript
gs = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py", line 848, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py", line 1441, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'gs'
【问题讨论】:
标签: python python-3.x turtle-graphics pillow