Hey guys,
Well I'm onto images now but the tutorial I'm using seems to be good but I can't understand the input process. And I know you guys are great at this stuff so thought you might lend a hand here to get me up and started. The code the guy in the tutorial is using goes like this.

import os, sys
import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext(infile)
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            Image.open(infile).save(outfile)
        except IOError:
            print "cannot convert", infile

So anyway just started messing around with it and I'm not getting any information into sys.argv[1:] using similar code, I commented out the ones I still wanna learn but won't use for now. It looks like this.

import os, sys
import Image
im = Image.open("D:\\pics\\jennifer.bmp")
#print im.format, im.size, im.mode
#im.show()

for im in sys.argv[1:]:
    f, e = os.path.splitext(im)
    outfile = f + ".jpg"
    if im != outfile:
        try:
            Image.open(im).save(outfile)
        except IOError:
            print "cannot convert", im

I guess its supposed to convert from one file type to another, but don't seem to be getting any information to the sys.argv[1:] module. So I guess my question is how does that work, and how can I get this up and running to test it?

Recommended Answers

All 3 Replies

sys.argv is used for passing command line arguments to your program. It is a list, and the first value sys.argv[0] is always the name of your program.

So let's say you open up a terminal and type in python my_prog.py foo bar Then sys.argv's contents would be
[0] my_prog.py
[1] foo
[2] bar

So by using slicing and saying sys.argv[1:] the author is getting everything except for the my_prog.py.

HTH

sys.argv is the arguments you give to the program when you run it from the command line. For example:

C:\Python> python image.py This_is_arg_0 This_is_arg_1

Hope that helps. What i think you have to do is give the program a location of some images to convert. That seems the most likely thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.