[Image-SIG] MemoryError

Scott David Daniels Scott.Daniels at Acm.Org
Wed Jul 9 04:55:02 CEST 2008


Shaun Edwards wrote:
> Hello, I'm still a bit new to all of this, and am extremely happy with 
> PIL.  However, I'm ending up with a slight memory issue....

...
 > ## For each file in every subdirectory, see if it's a tif file
 > for root, dir, files in os.walk(path):
 >    for name in files:
 >        filename, ext = os.path.splitext(name)
 >        if ext == ".tif":
 >            print 'Opening ' + name
 >            os.chdir(path)
This is a mistake.  os.walk does _not_ like you to chdir

 >            im = Image.open(root + '/' + name)
Better would be:
              im = Image.open(os.path.join(root, name))
....


I'd suggest something more like:
   for root, dir, files in os.walk(path):
      names = [name for name in in files if filename.endswith(".tif")]
      dest_dir = None
      for name in names:
          im = Image.open(os.path.join(root, name))
          if dest_dir is None:
              dest_dir = os.path.join('c' + root[1:], 'jpegs')
              try:
                  os.makedirs(dest_dir)
              except WindowsError, why:
                  print why
                  dest_dir = <some other place>
          x, y = im.size
          print 'Saving'
          im.resize((int(x*.66), int(y*.66)), Image.ANTIALIAS).save(
                       os.path.join(dest_dir, savedfile,) quality=85)


--Scott David Daniels
Scott.Daniels at Acm.Org




More information about the Image-SIG mailing list