-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[WIP] Add Utility function to read image from the file as imread is deprecated #10149
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
Closed
Closed
Changes from all commits
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Utility to read image from file as an array.""" | ||
import numpy as np | ||
|
||
# Try to import from PIL as imread is deprecated in SciPy 1.0.0, | ||
# and will be removed in 1.2.0. | ||
# Github Issue: https://github.com/scikit-learn/scikit-learn/issues/10147 | ||
try: | ||
try: | ||
from PIL import Image | ||
except ImportError: | ||
import Image | ||
except ImportError: | ||
raise ImportError("The Python Imaging Library (PIL) " | ||
"is required to load data from jpeg files") | ||
|
||
|
||
def _imread(name, flatten=False, mode=None): | ||
""" | ||
Read an image from a file as an array. | ||
This function is only available if Python Imaging Library (PIL) is installed. | ||
Parameters | ||
---------- | ||
name : str or file object | ||
The file name or file object to be read. | ||
flatten : bool, optional | ||
If True, flattens the color layers into a single gray-scale layer. | ||
mode : str, optional | ||
Mode to convert image to, e.g. ``'RGB'``. See the Notes for more | ||
details. | ||
Returns | ||
------- | ||
imread : ndarray | ||
The array obtained by reading the image. | ||
Notes | ||
----- | ||
`imread` uses the Python Imaging Library (PIL) to read an image. | ||
The following notes are from the PIL documentation. | ||
`mode` can be one of the following strings: | ||
* 'L' (8-bit pixels, black and white) | ||
* 'P' (8-bit pixels, mapped to any other mode using a color palette) | ||
* 'RGB' (3x8-bit pixels, true color) | ||
* 'RGBA' (4x8-bit pixels, true color with transparency mask) | ||
* 'CMYK' (4x8-bit pixels, color separation) | ||
* 'YCbCr' (3x8-bit pixels, color video format) | ||
* 'I' (32-bit signed integer pixels) | ||
* 'F' (32-bit floating point pixels) | ||
PIL also provides limited support for a few special modes, including | ||
'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa' | ||
(true color with premultiplied alpha). | ||
When translating a color image to black and white (mode 'L', 'I' or | ||
'F'), the library uses the ITU-R 601-2 luma transform:: | ||
L = R * 299/1000 + G * 587/1000 + B * 114/1000 | ||
When `flatten` is True, the image is converted using mode 'F'. | ||
When `mode` is not None and `flatten` is True, the image is first | ||
converted according to `mode`, and the result is then flattened using | ||
mode 'F'. | ||
""" | ||
im = Image.open(name) | ||
# Check if image is PIL | ||
if not Image.isImageType(im): | ||
raise TypeError("Input is not a PIL image.") | ||
return np.asarray(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.
You don't do anything here with
flatten
ormode
.