Python image library

Status
Not open for further replies.
I'm trying to iterate through all the pictures in a folder and resize them using the python thumbnail function. I get an error: IOError: cannot identify image file. Can anyone help me out on this one? It seems that it's not seeing the file as an image.

Code:
import Image, os
 
for filename in os.listdir('.'):
    img = Image.open(filename)
    img.thumbnail( (200,200) )
    out = file(os.path.splitext(filename)[0]+"_thumb.jpg", "w")
    try:
        img.save(out, "JPEG")
    finally:
        out.close()
 

dredphul

Ars Tribunus Angusticlavius
6,341
Subscriptor++
Debugging issues with scripts like this is easier if you have output to analyze.

For something as simple as the presented code, simply adding a 'print filename' statement would have gotten you past the first problem.

If you had outputted the file name as the script progressed, you'd know if a particular file couldn't be resized and you'd be able to look at the file and see if the file was corrupted or couldn't be processed due to some other issue.
 
dredphul":369wyg6l said:
Debugging issues with scripts like this is easier if you have output to analyze.

For something as simple as the presented code, simply adding a 'print filename' statement would have gotten you past the first problem.

If you had outputted the file name as the script progressed, you'd know if a particular file couldn't be resized and you'd be able to look at the file and see if the file was corrupted or couldn't be processed due to some other issue.

I did do that and it would print the name of the jpg file. I read that you should open it in binary mode but by that time I just went another route.
 

MooseAreFun

Ars Legatus Legionis
10,827
Subscriptor
I wish I could have done it myself in Python though.

You can still do that now.

Find one file which works and one which fails, open them both up in a good image editor which will tell you more about the file internals and see if you can find a difference in encoding or encoder or settings or something. Is there a PIL file size limit? What are the permissions like on the files? Does 'file'(?) identify it as a JPEG?
 
Status
Not open for further replies.