Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

autoclose files opened in load_img #261

Merged
merged 12 commits into from
Nov 26, 2019
54 changes: 28 additions & 26 deletions keras_preprocessing/image/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import division
from __future__ import print_function

import io
import os
import warnings

Expand Down Expand Up @@ -109,32 +110,33 @@ def load_img(path, grayscale=False, color_mode='rgb', target_size=None,
if pil_image is None:
raise ImportError('Could not import PIL.Image. '
'The use of `load_img` requires PIL.')
img = pil_image.open(path)
rragundez marked this conversation as resolved.
Show resolved Hide resolved
if color_mode == 'grayscale':
# if image is not already an 8-bit, 16-bit or 32-bit grayscale image
# convert it to an 8-bit grayscale image.
if img.mode not in ('L', 'I;16', 'I'):
img = img.convert('L')
elif color_mode == 'rgba':
if img.mode != 'RGBA':
img = img.convert('RGBA')
elif color_mode == 'rgb':
if img.mode != 'RGB':
img = img.convert('RGB')
else:
raise ValueError('color_mode must be "grayscale", "rgb", or "rgba"')
if target_size is not None:
width_height_tuple = (target_size[1], target_size[0])
if img.size != width_height_tuple:
if interpolation not in _PIL_INTERPOLATION_METHODS:
raise ValueError(
'Invalid interpolation method {} specified. Supported '
'methods are {}'.format(
interpolation,
", ".join(_PIL_INTERPOLATION_METHODS.keys())))
resample = _PIL_INTERPOLATION_METHODS[interpolation]
img = img.resize(width_height_tuple, resample)
return img
with open(path, 'rb') as f:
img = pil_image.open(io.BytesIO(f.read()))
if color_mode == 'grayscale':
# if image is not already an 8-bit, 16-bit or 32-bit grayscale image
# convert it to an 8-bit grayscale image.
if img.mode not in ('L', 'I;16', 'I'):
img = img.convert('L')
elif color_mode == 'rgba':
if img.mode != 'RGBA':
img = img.convert('RGBA')
elif color_mode == 'rgb':
if img.mode != 'RGB':
img = img.convert('RGB')
else:
raise ValueError('color_mode must be "grayscale", "rgb", or "rgba"')
if target_size is not None:
width_height_tuple = (target_size[1], target_size[0])
if img.size != width_height_tuple:
if interpolation not in _PIL_INTERPOLATION_METHODS:
raise ValueError(
'Invalid interpolation method {} specified. Supported '
'methods are {}'.format(
interpolation,
", ".join(_PIL_INTERPOLATION_METHODS.keys())))
resample = _PIL_INTERPOLATION_METHODS[interpolation]
img = img.resize(width_height_tuple, resample)
return img


def list_pictures(directory, ext=('jpg', 'jpeg', 'bmp', 'png', 'ppm', 'tif',
Expand Down
6 changes: 4 additions & 2 deletions keras_preprocessing/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def text_to_word_sequence(text,
text = text.lower()

if sys.version_info < (3,):
if isinstance(text, unicode):
translate_map = dict((ord(c), unicode(split)) for c in filters)
if isinstance(text, unicode): # noqa: F821
translate_map = dict(
(ord(c), unicode(split)) for c in filters # noqa: F821
)
text = text.translate(translate_map)
elif len(split) == 1:
translate_map = maketrans(filters, split * len(filters))
Expand Down
22 changes: 22 additions & 0 deletions tests/image/utils_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np
import pytest
import resource
import PIL

from keras_preprocessing.image import utils

Expand Down Expand Up @@ -326,5 +328,25 @@ def test_array_to_img_and_img_to_array():
img = utils.img_to_array(x, data_format='channels_last')


def write_sample_image(tmpdir):
im = utils.array_to_img(np.random.rand(1, 1, 3))
path = str(tmpdir / 'sample_image.png')
utils.save_img(path, im)
return path


def test_image_file_handlers_close(tmpdir):
path = write_sample_image(tmpdir)
max_open_files, _ = resource.getrlimit(resource.RLIMIT_NOFILE)
for i in range(max_open_files+1):
utils.load_img(path)


def test_load_img_returns_image(tmpdir):
path = write_sample_image(tmpdir)
im = utils.load_img(path)
assert isinstance(im, PIL.Image.Image)


if __name__ == '__main__':
pytest.main([__file__])