correctly closing file handles when calculating image hash