Title: Issue · GitHub
Open Graph Title: Issue · matplotlib/matplotlib
X Title: Issue · matplotlib/matplotlib
Description: matplotlib: plotting with Python. Contribute to matplotlib/matplotlib development by creating an account on GitHub.
Open Graph Description: matplotlib: plotting with Python. Contribute to matplotlib/matplotlib development by creating an account on GitHub.
X Description: matplotlib: plotting with Python. Contribute to matplotlib/matplotlib development by creating an account on GitHub.
Opengraph URL: https://github.com/matplotlib/matplotlib
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: fig.savefig broken with Python 3.13.0?","articleBody":"### Bug summary\n\nAfter 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. \n\n### Code for reproduction\n\n```Python\nGenerate typical matplotlib example plot, and try to save it in PNG.\nIt crashes for me on line:\n\nfig.savefig(\"test_filename.png\", facecolor=fig.get_facecolor(), transparent=False)\n```\n\n### Actual outcome\n\nCrash log:\n\n```\nTraceback (most recent call last):\n File \"C:\\Python313\\Lib\\site-packages\\PIL\\ImageFile.py\", line 554, in _save\n fh = fp.fileno()\n ^^^^^^^^^\nAttributeError: '_idat' object has no attribute 'fileno'\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"C:\\Python313\\Lib\\site-packages\\PIL\\Image.py\", line 2596, in save\n save_handler(self, fp, filename)\n ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\PIL\\PngImagePlugin.py\", line 1488, in _save\n ImageFile._save(\n ~~~~~~~~~~~~~~~^\n single_im,\n ^^^^^^^^^^\n cast(IO[bytes], _idat(fp, chunk)),\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n [ImageFile._Tile(\"zip\", (0, 0) + single_im.size, 0, rawmode)],\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n )\n ^\n File \"C:\\Python313\\Lib\\site-packages\\PIL\\ImageFile.py\", line 558, in _save\n _encode_tile(im, fp, tile, bufsize, None, exc)\n ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\PIL\\ImageFile.py\", line 585, in _encode_tile\n fp.write(data)\n ~~~~~~~~^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\PIL\\PngImagePlugin.py\", line 1132, in write\n self.chunk(self.fp, b\"IDAT\", data)\n ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\PIL\\PngImagePlugin.py\", line 1119, in putchunk\n fp.write(byte_data)\n ~~~~~~~~^^^^^^^^^^^\nOSError: [Errno 9] Bad file descriptor\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n File \"c:\\_repo\\tckit\\tckit_qvr.py\", line 767, in \u003cmodule\u003e\n fig.savefig(png_fn_small, facecolor=fig.get_facecolor(), transparent=False)\n ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\matplotlib\\figure.py\", line 3490, in savefig\n self.canvas.print_figure(fname, **kwargs)\n ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\matplotlib\\backend_bases.py\", line 2184, in print_figure\n result = print_method(\n filename,\n ...\u003c3 lines\u003e...\n bbox_inches_restore=_bbox_inches_restore,\n **kwargs)\n File \"C:\\Python313\\Lib\\site-packages\\matplotlib\\backend_bases.py\", line 2040, in \u003clambda\u003e\n print_method = functools.wraps(meth)(lambda *args, **kwargs: meth(\n ~~~~^\n *args, **{k: v for k, v in kwargs.items() if k not in skip}))\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\matplotlib\\backends\\backend_agg.py\", line 481, in print_png\n self._print_pil(filename_or_obj, \"png\", pil_kwargs, metadata)\n ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\matplotlib\\backends\\backend_agg.py\", line 430, in _print_pil\n mpl.image.imsave(\n ~~~~~~~~~~~~~~~~^\n filename_or_obj, self.buffer_rgba(), format=fmt, origin=\"upper\",\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n dpi=self.figure.dpi, metadata=metadata, pil_kwargs=pil_kwargs)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\matplotlib\\image.py\", line 1634, in imsave\n image.save(fname, **pil_kwargs)\n ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^\n File \"C:\\Python313\\Lib\\site-packages\\PIL\\Image.py\", line 2599, in save\n fp.close()\n ~~~~~~~~^^\nOSError: [Errno 9] Bad file descriptor\n```\n\n### Expected outcome\n\nOlder Python 3.9 renders and saves PNG image normally without any issues, using same code.\n\n### Additional information\n\nMy test environment, installed using latest pip.\n\n```\n\u003epip freeze\ncolorama==0.4.6\ncontourpy==1.3.1\ncycler==0.12.1\nfonttools==4.55.3\nkiwisolver==1.4.8\nmatplotlib==3.10.0\nnumpy==2.2.1\npackaging==24.2\npandas==2.2.3\npillow==11.1.0\npyparsing==3.2.1\npython-dateutil==2.9.0.post0\npython-vxi11==0.9\npytz==2024.2\nscipy==1.15.1\nsix==1.17.0\nstandard-xdrlib==3.13.0\ntzdata==2024.2\n```\n\n### Operating system\n\nWindows 10\n\n### Matplotlib Version\n\n3.10.0\n\n### Matplotlib Backend\n\ntkagg\n\n### Python version\n\n3.13.0\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\npip","author":{"url":"https://github.com/tin-","@type":"Person","name":"tin-"},"datePublished":"2025-01-14T21:34:26.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/29472/matplotlib/issues/29472"}
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:239ba167-708b-9f87-4e8b-31a805c050b2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B978:1F62C5:5F270C:8149AF:6A51EBBA |
| html-safe-nonce | 847ad7c7553d43ab5eb4bb122131857d051e215f224a41de521dbe074f911d27 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOTc4OjFGNjJDNTo1RjI3MEM6ODE0OUFGOjZBNTFFQkJBIiwidmlzaXRvcl9pZCI6IjQzMjUyNjEwMTIwODc0NjY5MzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fdbafd0651fc52028332983b758c56a35af17ff741ed5869de4639aae81e8a03 |
| hovercard-subject-tag | repository:1385122 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/matplotlib/matplotlib/issues/29472 |
| twitter:image | https://opengraph.githubassets.com/9261bf81ff8848b426c0823e67448cf51d35b63d999294ff18880398e6b5b195/matplotlib/matplotlib |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9261bf81ff8848b426c0823e67448cf51d35b63d999294ff18880398e6b5b195/matplotlib/matplotlib |
| og:image:alt | matplotlib: plotting with Python. Contribute to matplotlib/matplotlib development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-cache |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width