8000 [Bug]: fig.savefig broken with Python 3.13.0? · Issue #29472 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: fig.savefig broken with Python 3.13.0? #29472
Closed as not planned
Closed as not planned
@tin-

Description

@tin-

Bug summary

After upgrading from Python 3.9 to Python 3.13.0 on Windows x64 platform working python app that uses matplotlib is unable to save PNG images. Issue is related to Pillow it seems.

Code for reproduction

Generate typical matplotlib example plot, and try to save it in PNG.
It crashes for me on line:

fig.savefig("test_filename.png", facecolor=fig.get_facecolor(), transparent=False)

Actual outcome

Crash log:

Traceback (most recent call last):
  File "C:\Python313\Lib\site-packages\PIL\ImageFile.py", line 554, in _save
    fh = fp.fileno()
         ^^^^^^^^^
AttributeError: '_idat' object has no attribute 'fileno'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python313\Lib\site-packages\PIL\Image.py", line 2596, in save
    save_handler(self, fp, filename)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\site-packages\PIL\PngImagePlugin.py", line 1488, in _save
    ImageFile._save(
    ~~~~~~~~~~~~~~~^
        single_im,
        ^^^^^^^^^^
        cast(IO[bytes], _idat(fp, chunk)),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        [ImageFile._Tile("zip", (0, 0) + single_im.size, 0, rawmode)],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Python313\Lib\site-packages\PIL\ImageFile.py", line 558, in _save
    _encode_tile(im, fp, tile, bufsize, None, exc)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\site-packages\PIL\ImageFile.py", line 585, in _encode_tile
    fp.write(data)
    ~~~~~~~~^^^^^^
  File "C:\Python313\Lib\site-packages\PIL\PngImagePlugin.py", line 1132, in write
    self.chunk(self.fp, b"IDAT", data)
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\site-packages\PIL\PngImagePlugin.py", line 1119, in putchunk
    fp.write(byte_data)
    ~~~~~~~~^^^^^^^^^^^
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\_repo\tckit\tckit_qvr.py", line 767, in <module>
    fig.savefig(png_fn_small, facecolor=fig.get_facecolor(), transparent=False)
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\site-packages\matplotlib\figure.py", line 3490, in savefig
    self.canvas.print_figure(fname, **kwargs)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\site-packages\matplotlib\backend_bases.py", line 2184, in print_figure
    result = print_method(
        filename,
    ...<3 lines>...
        bbox_inches_restore=_bbox_inches_restore,
        **kwargs)
  File "C:\Python313\Lib\site-packages\matplotlib\backend_bases.py", line 2040, in <lambda>
    print_method = functools.wraps(meth)(lambda *args, **kwargs: meth(
                                                                 ~~~~^
        *args, **{k: v for k, v in kwargs.items() if k not in skip}))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\site-packages\matplotlib\backends\backend_agg.py", line 481, in print_png
    self._print_pil(filename_or_obj, "png", pil_kwargs, metadata)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\site-packages\matplotlib\backends\backend_agg.py", line 430, in _print_pil
    mpl.image.imsave(
    ~~~~~~~~~~~~~~~~^
        filename_or_obj, self.buffer_rgba(), format=fmt, origin="upper",
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        dpi=self.figure.dpi, metadata=metadata, pil_kwargs=pil_kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\site-packages\matplotlib\image.py", line 1634, in imsave
    image.save(fname, **pil_kwargs)
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python313\Lib\site-packages\PIL\Image.py", line 2599, in save
    fp.close()
    ~~~~~~~~^^
OSError: [Errno 9] Bad file descriptor

Expected outcome

Older Python 3.9 renders and saves PNG image normally without any issues, using same code.

Additional information

My test environment, installed using latest pip.

>pip freeze
colorama==0.4.6
contourpy==1.3.1
cycler==0.12.1
fonttools==4.55.3
kiwisolver==1.4.8
matplotlib==3.10.0
numpy==2.2.1
packaging==24.2
pandas==2.2.3
pillow==11.1.0
pyparsing==3.2.1
python-dateutil==2.9.0.post0
python-vxi11==0.9
pytz==2024.2
scipy==1.15.1
six==1.17.0
standard-xdrlib==3.13.0
tzdata==2024.2

Operating system

Windows 10

Matplotlib Version

3.10.0

Matplotlib Backend

tkagg

Python version

3.13.0

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0