I'm trying to create a simple script to pull EXIF information out from multiple jpeg files at once, but I'm getting "IOError: cannot identify image file" when trying to use a variable instead of an absolute path. Here's the code that I'm using:
import os
from PIL import Image, ExifTags
source = raw_input("Enter the path to scan")
os.chdir(source)
for root, dirs, files in os.walk(source):
for file_name in files:
img = Image.open(file_name)
exif = { ExifTags.TAGS[k]: vars for k, vars in img._getexif().items() if k in ExifTags.TAGS }
print exif
I have also tried using StringIO per a tip I noticed while Google searching my problem. The code is the same as above except I import StringIO and make the following change in the Image.open code:
img = Image.open(StringIO(file_name))
This did not fix the problem and I get the same error. If I give a path instead of a variable in Image.open, it does work correctly so I know the problem is trying to use a variable. Is there a way to do this that I'm missing?
file_namewhen you get "IOError: cannot identify image file"? If the file name is "readme.txt" or something, it makes sense that PIL wouldn't be able to open it as an image. Additionally, does your source folder have any nested folders? For example,os.walkmight identifylenna.jpgin the folder "source/pictures", and then Image.open(file_name) will fail, because it's only looking in the current active directory "source" for lenna.jpg, when it's actually in "source/pictures".