1

我正在尝试使用从命令行运行的 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 文件夹为空)。

4

1 回答 1

1

我强烈建议遵循这个过程:

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

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!
于 2013-08-13T07:46:25.977 回答