-1

From Python3, I need to call application and pass it several arguments, one of which is

700x500\>

So I do this:

rule_template=r"{}x{}{}"
factor = r"\>"
rule = rule_template.format(700,500,factor)
cmd = ["mogrify", "-resize", rule]
for file in os.listdir(folder):
    if file.endswith(".jpg") or file.endswith(".png"):
        ff = os.path.join(folder, file)
        subprocess.check_call(cmd + [ff])

And the application fails because the argument it recieves looks like

700x500\\> 

How to avoid double slashes in the output. Again, it is not an "on screen only" feature, the application I call recieves wrong command.

Here is the full function that fails.

This is the error line:

subprocess.CalledProcessError: Command '['mogrify', '-resize', '100x5\\>', '/tmp/111/de0b8660-b132-4874-b898-1ab4d4a24375/001.jpg']' returned non-zero exit status 1.

If I replace the "factor" variable with empty "", application works, but does not do what I need it to.

Application usage example:

mogrify -resize 100x5\> 001.jpg
  • There is only a single backslash in your `rule` variable, so that can't be the problem. As it is, this question is impossible to answer. – Aran-Fey Sep 17 '17 at 13:37
  • 2
    I'm guessing you're trying to escape the `>` with `\>` when you don't need to for the resize factor... just set factor to `>` as it'll already be escaped appropriately... – Jon Clements Sep 17 '17 at 13:43
  • read more that this here: [adding backslashes](https://stackoverflow.com/questions/2179493/adding-backslashes-without-escaping-python) – Daniel Heidemann Sep 17 '17 at 13:46
  • @Rawing The question only requires some familiarity with what `mogrify` expects as an argument to `-resize` to answer. – chepner Sep 17 '17 at 13:46

1 Answers1

3

Your problem is that you don't need the backslash. If you were calling this from the shell,

mogrify -resize 700x500\>

would be correct because the > needs to be escaped to prevent the shell from interpreting it as an output redirection character. However, you are (correctly) passing a list of separate arguments to check_call, bypassing the shell. mogrify itself just needs the string 700x500>, so all you need is

rule_template = "{}x{}{}"
factor = ">"
rule = rule_template.format(700, 500, factor)
cmd = ["mogrify", "-resize", rule]
for file in os.listdir(folder):
    if file.endswith(".jpg") or file.endswith(".png"):
        ff = os.path.join(folder, file)
        subprocess.check_call(cmd + [ff])

A specific example of a call to check_call would be

subprocess.check_call(["mogrify", "-resize", "700x500>", "foo.jpg"])  

Completely unrelated, you only need one call to endswith, as it can take a tuple of expected extensinons:

if file.endswith((".jpg", ".png")):       
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks, it works. ImageMagic manual I had contains lines with backslashes, so I was sure it expects "XxY\>" string as argument. –  Sep 17 '17 at 13:51
  • They were probably demonstrating actual command lines. Quoting as `mogrify -resize "700x500>" ...` would also work in the shell. – chepner Sep 17 '17 at 13:57