【问题标题】:Convert EPS images to JPG using Pillow使用 Pillow 将 EPS 图像转换为 JPG
【发布时间】:2014-08-14 16:02:24
【问题描述】:

我正在编写一个脚本,该脚本将生成一系列使用 python turtle 模块绘制的图像。在对此进行研究时,我发现我可能只能从海龟中获取 EPS 图像,而我已经成功地做到了这一点。为了快速演示,我不得不使用 Photoshop 批量转换图像。因此,我显然想在脚本中进行转换。

  1. 使用海龟绘制框架
  2. 在 EPS 中获取框架
  3. 将帧转换为 JPG
  4. 重复

脚本中有两个我尝试过的函数,以及 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


    【解决方案1】:

    它试图找到的gs 是它作为子进程调用的Ghostscript 实用程序。安装它,然后确保 gs 命令在您的 PATH 上。由于您使用的是 OS X,如果您使用 Homebrew,则可以通过它使用 ghostscript

    【讨论】:

    • 已经尝试安装ghostscript,但它说它只与python 2兼容。将再试一次,这次将gs命令添加到PATH
    • 通过将export PATH="/usr/local/bin/gs:$PATH" 添加到.bash_profileecho $PATH 将gs 添加到路径中显示gs 但我仍然遇到与以前相同的错误。
    • PATH 不是这样工作的。 linfo.org/path_env_var.html 如果您通过 Homebrew 或类似方式安装 ghostscript,它应该已经在适当的位置。听起来你可能尝试安装一个名为那个的 Python 包?
    • 看起来像是走错路了... :-)
    • 是的,我想我确实安装了一个名为的 python 包。我通过 brew 安装了 ghostscript,它显示在 brew list 中,但仍然遇到上面的错误。将尝试卸载 python 包并通过 homebrew 重新安装。
    猜你喜欢
    • 1970-01-01
    • 2010-10-28
    • 2013-11-20
    • 2018-11-17
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    相关资源
    最近更新 更多