Skip to content
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

[Doc]: replace usages of .imread with PIL.Image.open #25914

Open
story645 opened this issue May 18, 2023 · 4 comments
Open

[Doc]: replace usages of .imread with PIL.Image.open #25914

story645 opened this issue May 18, 2023 · 4 comments
Labels
Documentation: examples files in galleries/examples Documentation

Comments

@story645
Copy link
Member

Documentation Link

https://matplotlib.org/devdocs/api/image_api.html#matplotlib.image.imread

Problem

We flag the imread function with a note that it's not recommended, so we should follow our own guidance and replace usages of imread with PIL when possible.

Suggested improvement

Marking this as a good first issue since it's replacing the usage of imread in the code and examples with PIL.Image.open when possible.

@story645 story645 added Documentation Good first issue Open a pull request against these issues if there are no active ones! labels May 18, 2023
@timhoffm
Copy link
Member

T.b.d.: This is often used in a context manager:

    with cbook.get_sample_data('grace_hopper.jpg') as image_file:
        photo = plt.imread(image_file)

It should be simpler to only pass the path to Image.open(), which currently reads:

photo = PIL.image.open(cbook.get_sample_data('grace_hopper.jpg', asfileobj=False))

or

photo = PIL.image.open(Path(matplotlib.get_data_path()) / 'grace_hopper.jpg'))

Both of them are still not elegant.

I thus propose to extend cbook.get_sample_data() to support image files (.jpg, .png) natively by using Image.open internally, so that we can directly write:

photo = cbook.get_sample_data('grace_hopper.jpg')

@QuLogic
Copy link
Member

QuLogic commented May 19, 2023

get_sample_data returns more data than just images, and @tacaswell doesn't like return type instability (and how it would decide wasn't specified; a parameter or extension sniffing?), so you may want a new get_sample_image instead.

@story645 story645 removed the Good first issue Open a pull request against these issues if there are no active ones! label May 19, 2023
@story645
Copy link
Member Author

Pulling good first issue until we shake out what we want to do but I like 'sample_image'

@timhoffm
Copy link
Member

timhoffm commented May 19, 2023

Thinking about it, it may be an educational aspect to not hide PIL.Image.open() call. It is not obvious how to transfer get_sample_image() to your own image file. So

photo = PIL.image.open(Path(matplotlib.get_data_path()) / 'grace_hopper.jpg'))

seems like a reasonable solution. It would be slightly nicer if get_data_path() would return a pathlib.Path object, but we're not there and migrating is not straight forward.

@story645 story645 added the Documentation: examples files in galleries/examples label Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: examples files in galleries/examples Documentation
Projects
None yet
Development

No branches or pull requests

3 participants