From a422a4ff4e8330162ecc83abaf9cd43b4e30615a Mon Sep 17 00:00:00 2001 From: wiredfool Date: Thu, 19 Jun 2014 13:21:40 -0700 Subject: [PATCH] ensure files are closed --- PIL/ImageMorph.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/PIL/ImageMorph.py b/PIL/ImageMorph.py index e2c29e850..116b6d756 100644 --- a/PIL/ImageMorph.py +++ b/PIL/ImageMorph.py @@ -209,14 +209,15 @@ class MorphOp: def get_on_pixels(self, image): """Get a list of all turned on pixels in a binary image - Returns a list of tuples of (x,y) coordinates of all matching pixels.""" return _imagingmorph.get_on_pixels(image.im.id) def load_lut(self, filename): """Load an operator from an mrl file""" - self.lut = bytearray(open(filename,'rb').read()) + with open(filename,'rb') as f: + self.lut = bytearray(f.read()) + if len(self.lut)!= 8192: self.lut = None raise Exception('Wrong size operator file!') @@ -225,7 +226,8 @@ class MorphOp: """Load an operator save mrl file""" if self.lut is None: raise Exception('No operator loaded') - open(filename,'wb').write(self.lut) + with open(filename,'wb') as f: + f.write(self.lut) def set_lut(self, lut): """Set the lut from an external source"""