title: How can I use a variable instead of a path to open an image file using PIL in Python?
body: <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>img = Image.open(StringIO(file_name))
  </code></pre>
  
  <p>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?</p>