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

Test failure with Pillow 9.4 #82

Closed
avalentino opened this issue Jan 6, 2023 · 6 comments · Fixed by #84
Closed

Test failure with Pillow 9.4 #82

avalentino opened this issue Jan 6, 2023 · 6 comments · Fixed by #84

Comments

@avalentino
Copy link
Contributor

avalentino commented Jan 6, 2023

Code Sample, a minimal, complete, and verifiable piece of code

$ python3 -m pytest -k test_grid_germ

Problem description

After the update of Pillow to v9.4 (from v9.3) one of the tests fails.
The test has been detected on debian/sid but it is also reproducible in conda environments.
This issue seems not to be related to #57 because the output of the textsize function seems to be the same before and after the update to the new Pillow version.

txt_width, txt_height = draw.textsize(txt, font)

See also: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1028040

Expected Output

All tests pass.

Actual Result, Traceback if applicable

========================================================== test session starts ===========================================================
platform linux -- Python 3.11.1, pytest-7.2.0, pluggy-1.0.0+repack
rootdir: /home/antonio/debian/git/pycoast
plugins: hypothesis-6.61.0, arraydiff-0.5.0, astropy-0.10.0, mock-3.8.2, lazy-fixture-0.6.3, doctestplus-0.12.1, openfiles-0.5.0, filter-subpackage-0.1.2, remotedata-0.4.0, cov-4.0.0, astropy-header-0.2.2
collected 57 items / 56 deselected / 1 selected                                                                                          

pycoast/tests/test_pycoast.py F                                                                                                    [100%]

================================================================ FAILURES ================================================================
__________________________________________________ TestContourWriterPIL.test_grid_germ ___________________________________________________

self = <pycoast.tests.test_pycoast.TestContourWriterPIL object at 0x7f01782c6cd0>

    def test_grid_germ(self):
        """Check that issue #26 is fixed."""
        from pycoast import ContourWriterPIL
    
        result_file = os.path.join(os.path.dirname(__file__), "grid_germ.png")
        grid_img = Image.open(result_file)
        grid_data = np.array(grid_img)
        img = Image.new("RGB", (1024, 1024))
        proj4_string = "+proj=stere +ellps=bessel +lat_0=90.0 +lon_0=5.0 +lat_ts=50.0 +a=6378144.0 +b=6356759.0"
        area_extent = [-155100.436345, -4441495.37946, 868899.563655, -3417495.37946]
    
        area_def = (proj4_string, area_extent)
    
        cw = ContourWriterPIL(gshhs_root_dir)
    
        cw.add_coastlines(img, area_def, resolution="l", level=4)
        font = ImageFont.truetype(os.path.join(os.path.dirname(__file__), "test_data", "DejaVuSerif.ttf"), 16)
        cw.add_grid(
            img,
            area_def,
            (10.0, 10.0),
            (2.0, 2.0),
            font=font,
            fill="yellow",
            write_text=True,
            outline="red",
            minor_outline="white",
        )
    
        res = np.array(img)
>       assert fft_metric(grid_data, res), "Writing of grid to germ failed"
E       AssertionError: Writing of grid to germ failed
E       assert False
E        +  where False = fft_metric(array([[[0, 0, 0],\n        [0, 0, 0],\n        [0, 0, 0],\n        ...,\n        [0, 0, 0],\n        [0, 0, 0],\n        [0...     [0, 0, 0],\n        [0, 0, 0],\n        ...,\n        [0, 0, 0],\n        [0, 0, 0],\n        [0, 0, 0]]], dtype=uint8), array([[[0, 0, 0],\n        [0, 0, 0],\n        [0, 0, 0],\n        ...,\n        [0, 0, 0],\n        [0, 0, 0],\n        [0...     [0, 0, 0],\n        [0, 0, 0],\n        ...,\n        [0, 0, 0],\n        [0, 0, 0],\n        [0, 0, 0]]], dtype=uint8))

pycoast/tests/test_pycoast.py:214: AssertionError
============================================================ warnings summary ============================================================
pycoast/tests/test_pycoast.py::TestContourWriterPIL::test_grid_germ
pycoast/tests/test_pycoast.py::TestContourWriterPIL::test_grid_germ
pycoast/tests/test_pycoast.py::TestContourWriterPIL::test_grid_germ
pycoast/tests/test_pycoast.py::TestContourWriterPIL::test_grid_germ
  /home/antonio/debian/git/pycoast/pycoast/cw_base.py:170: DeprecationWarning: textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead.
    txt_width, txt_height = draw.textsize(txt, font)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================================================== short test summary info =========================================================
FAILED pycoast/tests/test_pycoast.py::TestContourWriterPIL::test_grid_germ - AssertionError: Writing of grid to germ failed
============================================== 1 failed, 56 deselected, 4 warnings in 3.71s ==============================================

Versions of Python, package at hand and relevant dependencies

Python 3.11
Pillow 9.4

@djhoese
Copy link
Member

djhoese commented Jan 6, 2023

Looking at test_grid_germ we now get:

tmpt3gdad40

Compare against the main version:

https://github.com/pytroll/pycoast/blob/main/pycoast/tests/grid_germ.png

You'll notice the "10E" labels move over about 1 (maybe 2) pixels to the right.

@djhoese
Copy link
Member

djhoese commented Jan 6, 2023

So print statements show that the inputs into PIL's draw.text are exactly the same between versions of Pillow so this is purely internal to Pillow.

I hacked in some code to say "if it is the 10E label then move the x coordinate over 1 pixel" and the tests pass. Now I need to figure out if this is expected in Pillow now (a fix) or a regression.

@djhoese
Copy link
Member

djhoese commented Jan 6, 2023

Adding:

x_pos = np.floor(x_pos)
y_pos = np.floor(y_pos)

in _engine_text_draw then the new Pillow passes all pycoast tests. So it seems the rounding used in pillow has changed. In my opinion it is for the better, but I'd like to be sure where that change happened.

@avalentino
Copy link
Contributor Author

Thanks @djhoese , very interesting finding!

@djhoese
Copy link
Member

djhoese commented Jan 6, 2023

Here it is:

python-pillow/Pillow#6722

So this change was intentional and for the better. @mraspaud @pnuu I think I need to regenerate a couple of the expected test result PNGs. How do you feel about that?

@mraspaud
Copy link
Member

mraspaud commented Jan 9, 2023

sounds good, thanks for digging into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants