Imagemagick使用subprocess.Popen在python脚本中转换错误

时间:2013-08-13 07:21:53

标签: python imagemagick subprocess

我正在尝试使用从命令行运行的python脚本生成透明背景图像,但我很难将所有参数传递给subprocess.Popen,以便Imagemagick的转换不会通过我的错误

这是我的代码:

# Import modules
import os
import subprocess as sp

# Define useful variables
fileList = os.listdir('.')
fileList.remove(currentScriptName)

# Interpret return code
def interpretReturnCode(returnCode) :
    return 'OK' if returnCode is 0 else 'ERROR, check the script'

# Create background images
def createDirectoryAndBackgroundImage() :
    # Ask if numbers-height or numbers-width before creating the directory
    numbersDirectoryType = raw_input('Numbers directory: type "h" for "numbers-height" or "w" for "numbers-width": ')
    if numbersDirectoryType == 'h' :
        # Create 'numbers-height'  directory
        numbersDirectoryName = 'numbers-height'
        numbersDirectory = interpretReturnCode(sp.call(['mkdir', numbersDirectoryName]))
        print '%s%s' % ('Create "numbers-height" directory...', numbersDirectory)
        # Create background images
        startNumber = int(raw_input('First number for the background images: '))
        endNumber = (startNumber + len(fileList) + 1)
        for x in range(startNumber, endNumber) :
            createNum = []
            print 'createNum just after reset and before adding things to it: ', createNum, '\n'
            print 'start' , x, '\n'
            createNum = 'convert -size 143x263  xc:transparent -font "FreeSans-Bold" -pointsize 22 -fill \'#242325\' "text 105,258'.split()
            createNum.append('\'' + str(x) + '\'"')
            createNum.append('-draw')
            createNum.append('./' + numbersDirectoryName + '/' + str(x) + '.png')   
            print 'createNum set up, createNum submittet to subprocess.Popen: ', createNum
            createNumImage = sp.Popen(createNum, stdout=sp.PIPE)
            createNumImage.wait()   
            creationNumReturnCode = interpretReturnCode(createNumImage.returncode)
            print '%s%s%s' % ('\tCreate numbers image...', creationNumReturnCode, '\n')
    elif numbersDirectoryType == 'w' :
        numbersDirectoryName = 'numbers-width'
        numbersDirectory = interpretReturnCode(sp.call(['mkdir', numbersDirectoryName]))
        print '%s%s' % ('Create "numbers-width" directory...', numbersDirectory)
        # Create background images
        startNumber = int(raw_input('First number for the background images: '))
        endNumber = (startNumber + len(fileList) + 1)
        for x in range(startNumber, endNumber) :
            createNum = []
            print 'createNum just after reset and before adding things to it: ', createNum, '\n'
            print 'start' , x, '\n'
            createNum = 'convert -size 224x122  xc:transparent -font "FreeSans-Bold" -pointsize 22-fill \'#242325\' "text 105,258'.split()
            createNum.append('\'' + str(x) + '\'"')
            createNum.append('-draw')
            createNum.append('./' + numbersDirectoryName + '/' + str(x) + '.png')   
            print 'createNum set up, createNum submittet to subprocess.Popen: ', createNum
            createNumImage = sp.Popen(createNum, stdout=sp.PIPE)
            createNumImage.wait()   
            creationNumReturnCode = interpretReturnCode(createNumImage.returncode)
            print '%s%s%s' % ('\tCreate numbers image...', creationNumReturnCode, '\n')
    else :
        print 'No such directory type, please start again'
        numbersDirectoryType = raw_input('Numbers directory: type "h" for "numbers-height" or "w" for "numbers-width": ')

为此我得到以下错误,每张图片:

convert.im6: unable to open image `'#242325'': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `'#242325'' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `"text': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `"text' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `105,258': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `105,258' @ error/constitute.c/ReadImage/544.
convert.im6: unable to open image `'152'"': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no decode delegate for this image format `'152'"' @ error/constitute.c/ReadImage/544.
convert.im6: option requires an argument `-draw' @ error/convert.c/ConvertImageCommand/1294.

我试图改变参数的顺序而没有成功,在Popen中使用shell = True(但是函数interpretReturCode在没有创建图像的情况下返回OK(number-heights文件夹为空)。

1 个答案:

答案 0 :(得分:1)

我强烈建议您遵循以下流程:

  1. 选择一个文件和目录
  2. 更改上述内容,以便sp.Popen替换为print语句
  3. 从命令行运行修改后的脚本
  4. 尝试使用命令行的打印命令输出
  5. 修改命令行直至其正常工作
  6. 修改脚本,直到它生成完全相同的命令行
  7. 将打印件更改回sp.Popen - 然后(如果您仍有问题:
  8. 尝试修改命令字符串以启动echo convert 你可以看到在参数期间发生了什么,如果有的话 由sp.Popen处理。
  9.   

    python文档中还有这个方便的提示:

    >>> import shlex, subprocess
    >>> command_line = raw_input()
    /bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
    >>> args = shlex.split(command_line)
    >>> print args
    ['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
    >>> p = subprocess.Popen(args) # Success!