ajknzhol
ajknzhol

Reputation: 6450

Python Subprocess Error

I am trying to implement a small Filtres using PIL and have come this far but i am stuck with this traceback.

Traceback (most recent call last):
  File "C:/Users/Ajay/PycharmProjects/pygram/test.py", line 5, in <module>
    pg.execute("convert {filename} -modulate 120,10,100 -fill '#222b6d' -colorize 20 -gamma 0.5 -contrast -contrast {filename}")
  File "C:\Users\Ajay\PycharmProjects\pygram\pygram.py", line 22, in execute
    error = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
  File "C:\Python27\lib\subprocess.py", line 575, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'convert git.jpg -modulate 120,10,100 -fill '#222b6d' -colorize 20 -gamma 0.5 -contrast -contrast git.jpg' returned non-zero exit status 4

Code

import subprocess, math, os, inspect
from PIL import Image

class PyGram:
    def __init__(self, filename):
        self.filename = filename
        self.im = False

    def image(self):
        if not self.im:
            self.im = Image.open(self.filename)
        return self.im

    def execute(self, command, **kwargs):
        default = dict(
            filename=self.filename,
            width=self.image().size[0],
            height=self.image().size[1]
        )
        format = dict(default.items() + kwargs.items())
        command = command.format(**format)
        error = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
        return error

    def colortone(self, color, level, type=0):
        arg0 = level
        arg1 = 100 - level
        if type == 0:
            negate = '-negate'
        else:
            negate = ''

        self.execute(
            "convert {filename} \( -clone 0 -fill '{color}' -colorize 100% \) \( -clone 0 -colorspace gray {negate} \) -compose blend -define compose:args={arg0},{arg1} -composite {filename}",
            color=color,
            negate=negate,
            arg0=arg0,
            arg1=arg1
        )

    def border(self, color='black', width=20):
        self.execute("convert {filename} -bordercolor {color} -border {bwidth}x{bwidth} {filename}",
                     color=color,
                     bwidth=width
        )

test.py

from pygram import PyGram

pg = PyGram("git.jpg")

pg.execute("convert {filename} -modulate 120,10,100 -fill '#222b6d' -colorize 20 -gamma 0.5 -contrast -contrast {filename}")
pg.border()

Please help, where i am doing wrong ?

Upvotes: -1

Views: 744

Answers (1)

Anthony Kong
Anthony Kong

Reputation: 40664

Your test is invalid.

You try to execute this string:

"convert {filename} -modulate 120,10,100 -fill '#222b6d' -colorize 20 -gamma 0.5 -contrast -contrast {filename}"

but you did not pass in the value of 'filename'. See here for more details: String Formatting in Python 3

Upvotes: 2

Related Questions