0

I am creating a Python script to act as a wrapper for some common image-related tasks that I have to do, and here's a part where I am stuck at:

There is a Bash script someone wrote for ImageMagick which is located here: http://www.fmwconcepts.com/imagemagick/autocolor/index.php

Essentially, it can only operate on a single file (I cannot pass *.jpg to it, (it fails when I do so) otherwise I wouldn't have this problem) so I need Python to run this line on every file:

~/autocolor.sh -m " + ColorMethod + " -c " + WorkingDirectory + InputFileName + " " + WorkingDirectory + OutputFileName

This is my current block of code:

elif RetouchOption == "04":
    ColorMethod = input("What method will you use (options are gamma, recolor, none)?: ")
    ClipMode = input("What clipping mode will you use (options are together or separate)?: ")
    for f in WorkingDirectory + "*.jpg":
        do
        os.system("sh ~/autocolor.sh -m " + ColorMethod + " -c " + WorkingDirectory + FileName + " " + WorkingDirectory + FileName)

WorkingDirectory is already established as a variable, and ColorMethod and ClipMode are established in that block. What I need to do is get a FileName variable (or some other way to make this code work).

Thanks for the help! Let me know if I didn't supply enough information. I heard os.system is not the preferred way of doing something like this, but it seems to work well so far in executing other commands I have in the same script, so I'll tackle changing that over at another time.

1

6 Answers 6

1

Here are some ideas:

from glob import glob
import subprocess

elif RetouchOption == "04":
    ColorMethod = input("What method will you use (options are gamma, recolor, none)?: ")
    ClipMode = input("What clipping mode will you use (options are together or separate)?: ")
    script = ["sh", "~/autocolor.sh"]
    method = "-m %s" % ColorMethod
    clipmode = "-c %s" % ClipMode
    for filename in glob("*.jpg"):
        subprocess.call(script + [method, clipmode, filename, filename])

glob is great, and subprocess is preferred over os.system as you surmised.

Note that glob without your WorkingDirectory uses the 'current working directory of the script'. To make it use WorkingDirectory, try something like this:

import os

for filename in glob(os.path.join(WorkingDirectory, '*.jpg')):
    ...
7
  • Wow that really cleaned up my snippet. I have a lot to learn about Python, I'll be giving this a shot! May 3, 2011 at 18:03
  • 1
    @Mylan Another cleanup item - notice that StackOverflow syntax highlights camel case items like ColorMethod - that's because it thinks it's a class name. Try using a different convention like color_method or colorMethod so everyone knows it isn't a type/class
    – Henry
    May 3, 2011 at 18:08
  • Thanks Henry, I'll get on that as well. I appreciate all the help. One problem: the line for filename in glob(os.path.join(WorkingDirectory, '.jpg')): is giving me an error: 'code' Traceback (most recent call last): File "/home/mylan/Image-QA-0.3.py", line 111, in <module> for filename in glob(os.path.join(WorkingDirectory, '.jpg')): TypeError: 'module' object is not callable ... any ideas? May 3, 2011 at 18:38
  • You probably did import glob rather than from glob import glob - glob is both the module name and the name of the function. Can't be 100% sure though.
    – Henry
    May 3, 2011 at 18:44
  • 1
    I think you should split "sh ~/autocolor.sh" into two strings, so you call it: subprocess.call(["/bin/sh","/home/mylan/autocolor.sh", method, clipmode, filename, filename])
    – tmg
    May 3, 2011 at 20:18
1

Isn't it better to use bash?

for f in *.jpg; do ./autocolor.sh $f; done

3
  • Yes, but what if you just LOVE Python :) Also it looks like there is more logic going on there with the elif "04" thing.
    – Henry
    May 3, 2011 at 18:08
  • Well, consider this "Essentially, it can only operate on a single file (I cannot pass *.jpg to it, (it fails when I do so) otherwise I wouldn't have this problem) so I need Python to run this line on every file:" - so, I assume it's not about Python LOVE, but just about no idea how to run loop in bash...
    – tmg
    May 3, 2011 at 19:56
  • I was actually referring to my love of Python, which is why I was happy to respond with information about Python. I was serious about the extra logic though - the line elif RetouchOption == "04": leads me to believe that this script might have importance outside of just looping. I think it's great that the question leads to varied answers, and it looks like yours is at the top so the best meme does win :)
    – Henry
    May 3, 2011 at 19:59
0
for f in glob.glob('*.jpg'):
    do_something() 
0

The Python glob module makes it easy to create lists that match a pattern.

Example:

    import glob
    glob.glob("*txt")

The above code would return a list of all "txt" files in the current directory.

http://docs.python.org/library/glob.html

0

Have you considered using os.listdir to get all the files in the directory?

Once you have a listing of all files in the directory, you can pull out all the JPGs and use the following code to to remove the ".jpg" from the end of the filename:

>>> f = 'firstPic.jpg'
>>> f. replace(".jpg", '')
'firstPic'

Hope this helps

0
0

This is a suggestion for coding. I write a lot of Python code that works with MySQL. I have found it helpful as a debugging aid to create the string in a variable and print it out, so you can see what the script is doing.

You will do well with earlier answers to your question. They are quite helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.