Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update image tutorial. #23889

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

Merged
merged 1 commit into from
Sep 15, 2022
Merged

Update image tutorial. #23889

merged 1 commit into from
Sep 15, 2022

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Sep 14, 2022

  • Use Pillow to load the image rather than the discouraged plt.imread.
  • Remove discussion about rescaling to 0-1 as dropping the use of plt.imread also gets rid of that (not so nice) behavior. Also drop the lengthy discussion about uint8 and float32, which seems out of place here (if anything it should be in the Pillow docs). Make corresponding fixes to code to use 0-255 instead of 0-1.
  • Fix subplot management to go full pyplot (the local switch to OO-style is a bit weird).
  • Fix interpolation discussion as the default is now "nearest" (actually it's now "antialiased", but let's sweep that under the rug for this tutorial for now).

See #22790. Probably the tutorial could be further improved, but let's consider this a small step in that direction.

PR Summary

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

Comment on lines 82 to 84
# Here, we used Pillow to open an image (with `PIL.Image.open`), and
# immediately converted the `PIL.Image.Image` object into a 8-bit
# (``dtype=uint8``) numpy array.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Here, we used Pillow to open an image (with `PIL.Image.open`), and
# immediately converted the `PIL.Image.Image` object into a 8-bit
# (``dtype=uint8``) numpy array.
# We use Pillow to open an image (with `PIL.Image.open`), and
# immediately convert the `PIL.Image.Image` object into an 8-bit
# (``dtype=uint8``) numpy array.

And this should maybe be before the code instead of and here we go

@@ -205,20 +199,19 @@
#
# You can specify the clim in the call to ``plot``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# You can specify the clim in the call to ``plot``.
# You can specify the clim (colormap limits) in the call to ``plot``.

I think clim is colormap limits?

Comment on lines 213 to 214
ax = fig.add_subplot(1, 2, 1)
plt.subplot(1, 2, 1)
imgplot = plt.imshow(lum_img)
ax.set_title('Before')
plt.colorbar(ticks=[0.1, 0.3, 0.5, 0.7], orientation='horizontal')
ax = fig.add_subplot(1, 2, 2)
plt.title('Before')
plt.colorbar(orientation='horizontal')
plt.subplot(1, 2, 2)
imgplot = plt.imshow(lum_img)
imgplot.set_clim(0.0, 0.7)
ax.set_title('After')
plt.colorbar(ticks=[0.1, 0.3, 0.5, 0.7], orientation='horizontal')
imgplot.set_clim(0, 175)
plt.title('After')
plt.colorbar(orientation='horizontal')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should stay OO since that's what we usually recommend, but with an fig, (ax1m ax2) = plt.subplots( ncols=2)

all the other examples are pyplot 'cause it's only a line of code and I think that's a fairly clear distinction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just got rid of the subplots, keeping only the second plot seems good enough.

img = Image.open('../../doc/_static/stinkbug.png')
img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
imgplot = plt.imshow(img)

###############################################################################
# Here we have the default interpolation, bilinear, since we did not
# Here we have the default interpolation ("nearest"), since we did not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Here we have the default interpolation ("nearest"), since we did not
# Here we use the default interpolation ("nearest"), since we did not

@tacaswell tacaswell added this to the v3.7.0 milestone Sep 14, 2022
@@ -205,20 +199,19 @@
#
# You can specify the clim in the call to ``plot``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And its imshownotplot`, we are talking about.

Then maybe:

Suggested change
# You can specify the clim in the call to ``plot``.
# You can specify the colormap limits using ``imshow(..., clim=...)``.

Sorry, to comment on code you're not responsible for, but this is too glaringly wrong.

@@ -242,19 +235,17 @@
# We'll use the Pillow library that we used to load the image also to resize
# the image.

from PIL import Image

img = Image.open('../../doc/_static/stinkbug.png')
img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
img.thumbnail((64, 64)) # resizes image in-place

May just as well put that here instead of #23881 (which can be closed then).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged yours.

@anntzer
Copy link
Contributor Author

anntzer commented Sep 15, 2022

Thanks, handled all comments (got rid of the subplots, as noted re: @story645's suggestion).

- Use Pillow to load the image rather than the discouraged plt.imread.
- Remove discussion about rescaling to 0-1 as dropping the use of
  plt.imread also gets rid of that (not so nice) behavior.  Also drop
  the lengthy discussion about uint8 and float32, which seems out of
  place here (if anything it should be in the Pillow docs).  Make
  corresponding fixes to code to use 0-255 instead of 0-1.
- Fix subplot management to go full pyplot (the local switch to
  OO-style is a bit weird).
- Fix interpolation discussion as the default is now "nearest" (actually
  it's now "antialiased", but let's sweep that under the rug for this
  tutorial for now).
@timhoffm timhoffm merged commit 2ff4dc9 into matplotlib:main Sep 15, 2022
@anntzer anntzer deleted the imtut branch September 15, 2022 12:36
melissawm pushed a commit to melissawm/matplotlib that referenced this pull request Dec 19, 2022
- Use Pillow to load the image rather than the discouraged plt.imread.
- Remove discussion about rescaling to 0-1 as dropping the use of
  plt.imread also gets rid of that (not so nice) behavior.  Also drop
  the lengthy discussion about uint8 and float32, which seems out of
  place here (if anything it should be in the Pillow docs).  Make
  corresponding fixes to code to use 0-255 instead of 0-1.
- Fix subplot management to go full pyplot (the local switch to
  OO-style is a bit weird).
- Fix interpolation discussion as the default is now "nearest" (actually
  it's now "antialiased", but let's sweep that under the rug for this
  tutorial for now).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants