使用Pillow将EPS图像转换为JPG

1 投票
1 回答
3730 浏览
提问于 2025-04-18 17:18

我正在写一个脚本,用来生成一系列用Python的turtle模块绘制的图像。在研究这个过程中,我发现我可能只能从turtle中得到EPS格式的图像,而我已经成功做到了这一点。为了快速演示,我不得不使用Photoshop批量转换这些图像。因此,我当然希望能在脚本中直接完成这个转换。

  1. 用turtle绘制一个框架
  2. 将框架保存为EPS格式
  3. 把框架转换为JPG格式
  4. 重复以上步骤

在脚本中,我尝试了两个函数,还有一个在for循环中的简化转换代码。第一个函数会产生错误信息,而第二个函数则没有任何反应。在for循环中的简化转换代码里,我找到了导致问题的那一行,可能和保存方法有关。

我使用的是最新版本的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'

1 个回答

2

它试图找到的 gs 是一个叫做 Ghostscript 的工具,它会作为一个子程序被调用。你需要安装这个工具,并确保安装后 gs 命令可以在你的系统路径中找到。因为你使用的是 OS X,如果你用 Homebrew 来管理软件的话,可以通过它来安装 ghostscript

撰写回答