@@ -165,8 +165,8 @@ def read_image_gray(fname,pageno=0):
165
165
the range 0...1 (unsigned) or -1...1 (signed)."""
166
166
if type (fname )== tuple : fname ,pageno = fname
167
167
assert pageno == 0
168
- pil = PIL .Image .open (fname )
169
- a = pil2array (pil )
168
+ with PIL .Image .open (fname ) as pil :
169
+ a = pil2array (pil )
170
170
if a .dtype == dtype ('uint8' ):
171
171
a = a / 255.0
172
172
if a .dtype == dtype ('int8' ):
@@ -202,8 +202,8 @@ def read_image_binary(fname,dtype='i',pageno=0):
202
202
of the given dtype."""
203
203
if type (fname )== tuple : fname ,pageno = fname
204
204
assert pageno == 0
205
- pil = PIL .Image .open (fname )
206
- a = pil2array (pil )
205
+ with PIL .Image .open (fname ) as pil :
206
+ a = pil2array (pil )
207
207
if a .ndim == 3 : a = amax (a ,axis = 2 )
208
208
return array (a > 0.5 * (amin (a )+ amax (a )),dtype )
209
209
@@ -256,8 +256,8 @@ def make_seg_white(image):
256
256
def read_line_segmentation (fname ):
257
257
"""Reads a line segmentation, that is an RGB image whose values
258
258
encode the segmentation of a text line. Returns an int array."""
259
- pil = PIL .Image .open (fname )
260
- a = pil2array (pil )
259
+ with PIL .Image .open (fname ) as pil :
260
+ a = pil2array (pil )
261
261
assert a .dtype == dtype ('B' )
262
262
assert a .ndim == 3
263
263
image = rgb2int (a )
@@ -276,8 +276,8 @@ def write_line_segmentation(fname,image):
276
276
def read_page_segmentation (fname ):
277
277
"""Reads a page segmentation, that is an RGB image whose values
278
278
encode the segmentation of a page. Returns an int array."""
279
- pil = PIL .Image .open (fname )
280
- a = pil2array (pil )
279
+ with PIL .Image .open (fname ) as pil :
280
+ a = pil2array (pil )
281
281
assert a .dtype == dtype ('B' )
282
282
assert a .ndim == 3
283
283
segmentation = rgb2int (a )
0 commit comments