1

I'm reading a folder with different image types and if they are not in the jpeg format then to save them in this format.

But when I try to save them in this format I get an IOError. I've confirmed that I am reading the file contents ok:

I'm trying to do this using the PIL library. This is the code:

folder = os.listdir("C:\***\***\***\\abc")
for infile in folder:
    f,e = os.path.splitext(infile)
    newFile = f + ".jpg"
    if infile != newFile:
        try:
            Image.open(infile).save(newFile)
            print "DONE"
        except IOError:
            print "Cannot convert file", infile
    else:
        print "File: " + f + " is in jpg format"

Checking output here

folder= os.listdir("C:\***\***\***\\abc")
print folder
for infile in folder:
    print infile 

Currently I get the error message:

Image.open(infile).save(newFile)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1952, in open
fp = __builtin__.open(fp, "rb")
IOError: [Errno 2] No such file or directory: 'empire.bmp'

This is pretty much the code from the resource at: http://effbot.org/imagingbook /introduction.htm

Thanks

user94628
  • 3,197
  • 15
  • 46
  • 83

2 Answers2

3

May be your infile was not recognized, try this :

originFile = 'C:\***\***\***\\abc\%s' % infile
Image.open(originFile).save(newFile)
yakiang
  • 1,528
  • 1
  • 15
  • 17
1

Try using pgmagick library, I always used for this task and it always did the job for me. It's really easy to use. Read the image whatever format it has and save it as jpg (by replacing the extension in the path)

qurban
  • 3,529
  • 18
  • 33