使用Pillow将EPS图像转换为JPG

时间:2014-08-14 16:02:24

标签: python python-3.x turtle-graphics pillow

我正在编写一个脚本,它将生成一系列使用python turtle模块绘制的图像。在研究这一点时,我发现我可能只能从我成功做的龟中获取EPS图像。为了快速演示这个,我不得不使用Photoshop批量转换图像。因此,我很乐意在剧本中进行转换。

  1. 使用乌龟绘制框架
  2. 获取EPS中的框架
  3. 将帧转换为 JPG
  4. 重复
  5. 在脚本中有两个我尝试过的函数以及for循环中的简化转换代码。第一个函数产生错误消息,第二个函数什么都不做。在for循环中的简化转换代码中,我已经识别出导致问题的行,这可能与save方法有关。

    我使用最新版本的Pillow with 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 个答案:

答案 0 :(得分:1)

它尝试查找的gsGhostscript实用程序,它将其作为子进程调用。安装它,然后确保gs命令在PATH上。由于您使用的是OS X,因此如果您使用自制程序,则可以使用ghostscript