Fix to close files as soon as possible
make id a local variable
Merge pull request #1 from kaeza/master

Fix image files being left open and made id a local variable