Skip to content

PIL-opened image and imread-opened image render slightly differently in classic mode #14340

Open

Description

@anntzer

Bug report

Bug summary

Per the title.

Code for reproduction

from pathlib import Path
import matplotlib as mpl
from matplotlib.testing.compare import compare_images
from matplotlib import pyplot as plt, style
from PIL import Image

mpl.use("agg")
mpl.style.use("classic")

path = str(Path(mpl.__file__).parent / "tests/baseline_images/pngsuite/basn3p04.png")

fig = plt.figure()
fig.subplots().imshow(plt.imread(path))
fig.savefig("1.png")
fig = plt.figure()
fig.subplots().imshow(Image.open(path))
fig.savefig("2.png")
print(compare_images("1.png", "2.png", tol=0))

Actual outcome

Error: Image files did not match.
  RMS Value: 0.010865337342004415
  Expected:  
    1.png
  Actual:    
    2.png
  Difference:
    2-failed-diff.png
  Tolerance: 
    0

Expected outcome

No diff.

I think this may possibly(???) be due to rounding errors when imread converts from 0-255 (uint8) to 0-1 (float) and then imshow converts back to 0-255 (uint8)?

Note that this does not occur when using the mpl20 default style.

Matplotlib version

  • Operating system: linux
  • Matplotlib version: master
  • Matplotlib backend (print(matplotlib.get_backend())): agg
  • Python version: 3.7
  • Jupyter version (if applicable):
  • Other libraries:

Activity

  1. github-actions commented on Jun 19, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  2. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Jun 19, 2023
  3. QuLogic commented on Feb 2, 2024

    @QuLogic
    Member

    This seems to be related to the interpolation mode:

    from pathlib import Path
    import matplotlib as mpl
    from matplotlib.testing.compare import compare_images
    from matplotlib import pyplot as plt
    from PIL import Image
    
    mpl.use("agg")
    
    path = str(Path(mpl.__file__).parent / "tests/baseline_images/pngsuite/basn3p04.png")
    
    for interpolation in ['antialiased', 'bilinear']:
        mpl.rcParams['image.interpolation'] = interpolation
        fig = plt.figure()
        fig.subplots().imshow(plt.imread(path))
        fig.savefig(f"{interpolation}1.png")
        fig = plt.figure()
        fig.subplots().imshow(Image.open(path))
        fig.savefig(f"{interpolation}2.png")
        print(interpolation,
              compare_images(f"{interpolation}1.png", f"{interpolation}2.png", tol=0))
    antialiased None
    bilinear Error: Image files did not match.
      RMS Value: 0.01679637030895531
      Expected:
        bilinear1.png
      Actual:
        bilinear2.png
      Difference:
        bilinear2-failed-diff.png
      Tolerance:
        0
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    keepItems to be ignored by the “Stale” Github Actiontopic: images

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions