New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Force closing PIL image files #972
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1185,12 +1185,20 @@ def imread(fname, format=None): | |
can be used with :func:`~matplotlib.pyplot.imshow`. | ||
""" | ||
|
||
def pilread(): | ||
def pilread(fname): | ||
"""try to load the image with PIL or return None""" | ||
try: from PIL import Image | ||
except ImportError: return None | ||
image = Image.open( fname ) | ||
return pil_to_array(image) | ||
try: | ||
from PIL import Image | ||
except ImportError: | ||
return None | ||
if cbook.is_string_like(fname): | ||
# force close the file after reading the image | ||
with open(fname, "rb") as fp: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
image = Image.open(fp) | ||
return pil_to_array(image) | ||
else: | ||
image = Image.open(fname) | ||
return pil_to_array(image) | ||
|
||
handlers = {'png' :_png.read_png, } | ||
if format is None: | ||
|
@@ -1206,7 +1214,7 @@ def pilread(): | |
ext = format | ||
|
||
if ext not in handlers.iterkeys(): | ||
im = pilread() | ||
im = pilread(fname) | ||
if im is None: | ||
raise ValueError('Only know how to handle extensions: %s; with PIL installed matplotlib can handle more images' % handlers.keys()) | ||
return im | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you have extended the capability of this function (file handles are now supported), can you improve the doctoring a little. Perhaps the variable name
fname
is no longer appropriate - maybe something likeimg_file
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the capability of the function is the same as before. It just works around another bug in PIL, which does not seem to close all the files it opens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. but the variable naming statement still holds (
fname
could be a file handle or a filename).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it is a good idea to rename the variable.
fname
is used as an argument in the outer function. Renaming it there would break the API. Renaming it inpilread
only would be confusing I think.