Repository navigation
Closed
Labels
Description
What did you do?
Try to automate testing all supported save formats.
What did you expect to happen?
Use the special needs of the specific extensions like BW of SGI only accepting mode L. (Why don't the plugins use Pillow to convert the mode themselves?)
What actually happened?
python -m doctest main.py
**********************************************************************
File "d:\code\image viewer\tk_image_viewer\main.py", line 1157, in main.test_save
Failed example:
test_save()
Exception raised:
Traceback (most recent call last):
File "C:\Users\C\AppData\Local\Python\pythoncore-3.13-64\Lib\doctest.py", line 1398, in __run
exec(compile(example.source, filename, "single",
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
compileflags, True), test.globs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<doctest main.test_save[0]>", line 1, in <module>
test_save()
~~~~~~~~~^^
File "d:\code\image viewer\tk_image_viewer\main.py", line 1163, in test_save
im.save(out, format=ext)
~~~~~~~^^^^^^^^^^^^^^^^^
File "D:\code\image viewer\tk_image_viewer\venv\Lib\site-packages\PIL\Image.py", line 2575, in save
save_handler = SAVE[format.upper()]
~~~~^^^^^^^^^^^^^^^^
KeyError: '.APNG'
**********************************************************************
1 item had failures:
1 of 1 in main.test_save
***Test Failed*** 1 failure.What are your OS, Python and Pillow versions?
- OS: Windows 11
- Python: Python 3.13 (in active venv)
- Pillow: 12.1.0
Please paste here the output of running:
python3 -m PIL.report
--------------------------------------------------------------------
Pillow 12.1.0
Python 3.14.2 (tags/v3.14.2:df79316, Dec 5 2025, 17:18:21) [MSC v.1944 64 bit (AMD64)]
--------------------------------------------------------------------
Python executable is C:\Users\C\AppData\Local\Python\pythoncore-3.14-64\python.exe
System Python files loaded from C:\Users\C\AppData\Local\Python\pythoncore-3.14-64
--------------------------------------------------------------------
Python Pillow modules loaded from C:\Users\C\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\PIL
Binary Pillow modules loaded from C:\Users\C\AppData\Local\Python\pythoncore-3.14-64\Lib\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 12.1.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.14.1
--- LITTLECMS2 support ok, loaded 2.17
--- WEBP support ok, loaded 1.6.0
--- AVIF support ok, loaded 1.3.0
--- JPEG support ok, compiled for libjpeg-turbo 3.1.3
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.4
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1.zlib-ng, compiled for zlib-ng 2.3.2
--- LIBTIFF support ok, loaded 4.7.1
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
python -m PIL.report
--------------------------------------------------------------------
Pillow 12.1.0
Python 3.13.12 (tags/v3.13.12:1cbe481, Feb 3 2026, 18:22:25) [MSC v.1944 64 bit (AMD64)]
--------------------------------------------------------------------
Python executable is D:\code\image viewer\tk_image_viewer\venv\Scripts\python.exe
Environment Python files loaded from D:\code\image viewer\tk_image_viewer\venv
System Python files loaded from C:\Users\C\AppData\Local\Python\pythoncore-3.13-64
--------------------------------------------------------------------
Python Pillow modules loaded from D:\code\image viewer\tk_image_viewer\venv\Lib\site-packages\PIL
Binary Pillow modules loaded from D:\code\image viewer\tk_image_viewer\venv\Lib\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 12.1.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.14.1
--- LITTLECMS2 support ok, loaded 2.17
--- WEBP support ok, loaded 1.6.0
--- AVIF support ok, loaded 1.3.0
--- JPEG support ok, compiled for libjpeg-turbo 3.1.3
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.4
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1.zlib-ng, compiled for zlib-ng 2.3.2
--- LIBTIFF support ok, loaded 4.7.1
--- RAQM (Bidirectional Text) support ok, loaded 0.10.3, fribidi 1.0.13, harfbuzz 12.3.0
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
#TIL venv doesn't override "python3" as command. Sad.
from io import BytesIO
from PIL import Image
im = Image.open("eye.gif")
out = BytesIO()
for ext in ("APNG", "BW"):
im.save(out, ext)
Activity
radarhere commentedon Feb 11, 2026
In your error trace, you're showing
im.save(out, format=ext)andKeyError: '.APNG'. Theformatargument fromsave()is not intended to take an extension, but a format. In this case, it is "PNG"If I adjust your code to
im.save(out, "PNG"), it works. Pillow will automatically give you an APNG image if there is more than one frame.You may be interested in #2663, an open issue with a suggestion for how to make this easier.
CTimmerman commentedon Feb 11, 2026
I tried "APNG" before, which failed. As Pillow usually parses the extension anyway, I figured it might work as the format argument as well. Alas, even after providing file names, it appears that Pillow needs a real file:
[-]Can't save APNG to BytesIO[/-][+]Can't save all extensions to BytesIO[/+]radarhere commentedon Feb 11, 2026
To be clear,
works with Pillow 12.1.0 and Python 3.13.
Your extended code example is shows bar.apng working successfully. So now you're asking about bar.bmp. Testing, I find this is no longer a problem when I remove the use of pyfakefs.
If pyfakefs isn't working where saving to a normal path does, that sounds like an issue to raise with https://github.com/pytest-dev/pyfakefs
CTimmerman commentedon Feb 11, 2026
Then why did it work on the extensions before bmp? Anyway, I just want to test all extensions without writing to disk.
radarhere commentedon Feb 11, 2026
This would be my suggestion for saving files in all formats.
If you do explicitly want to use an extension without saving to disk, this would be my suggestion.
CTimmerman commentedon Feb 11, 2026
I googled that but could not find it used or documented (and I disabled member warnings for my global hacks). Thanks!
Why then the format parameter, and why does that not accept "BW" for example?
radarhere commentedon Feb 11, 2026
The
formatparameter has been on thesave()method since we forked from PIL, so I can't give you any kind of official explanation.My guess would be that a file extension was thought of as just as an an alias for the image format - who cares if it is
.tifor.tiff? If there's no filename, the exact extension is usually meaningless, and what really matters is what type of image it is, so let's have one single identifier for that, theformat. It's perhaps also worth noting that the SPIDER image format doesn't have any extensions, so we couldn't rely purely on extensions.".bw" is a file extension for the "SGI" format. We could make the
formatparameter more flexible, to also accept extensions, but without a concrete need for that functionality, the additional possibility seems confusing, rather than clarifying, for new users. There's already a clear mapping from file extensions to formats in the form ofImage.EXTENSION, and I can't recall this ever previously being raised.CTimmerman commentedon Feb 11, 2026
I don't think <24-bit images are as popular as they once were, and even with the BytesIO instance
out.name = "bla.bw",im.save(out)fails withValueError: Unsupported SGI image mode, so if the SGI format covers the BW extension, it should autoconvert to mode L.It accepts and writes mode RGB but SGI extension ".bw" should be mode L. And SGI extension ".rgba" should be RGBA.
radarhere commentedon Feb 12, 2026
I think you're making two requests.
Not all image conversion is lossless, and losing data without notifying the user doesn't sound ideal to me. We do so for GIF at least, but I'm not sure it should be universal. Feel free to see if others disagree with me. As I said, I think it worth looking at #2663, an open issue with a suggestion for how to make this easier.
We do use the filename extension for the image type when saving JPEG2000 images (not the mode), but that's the only extension-based plugin-specific data handling that I'm aware of.
I think it's a reasonably fundamental concept of computing that users can name their files whatever they like. I just opened GIMP and exported a JPEG with a '.png' extension. Granted, it made me confirm what I was doing, but it still allowed me to do so in the end.
It also seems a little odd given that you just went through effort to be able to save images without a filename. Earlier in this issue you tried to save a single frame image as an APNG. Conceptually, I'm not sold that images meaningfully have, or need, an extension if they don't have a filename.
CTimmerman commentedon Feb 12, 2026
I've upvoted #2663 but as the issue is from 2017, it might take another year or 5 to get merged, so I've already written my own conversion code that doesn't bother the user who should know that ".bw" is for black and white files (probably why the user typed it in the first place). Magic bytes require reading the file, and MIME types require a confusing and ugly separate metadata channel like MacOS's __MACOSX folder.
As the issue with testing without burdening the disk is resolved by using the undocumented name property of a BytesIO object, this issue can remain as a bug for putting RGB in a BW and similar unexpected situations.
radarhere commentedon Feb 13, 2026
While it would nice if all file extensions were correct, I think Pillow acknowledges that this may not be the case. https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#image-file-formats
We allow a JPEG to be saved with a '.png' extension.
CTimmerman commentedon Feb 13, 2026
Fine as you're reading the file anyway, but
im.save("out.sgi", "BW")doesn't work.radarhere commentedon Feb 13, 2026
I think this has already been discussed.
#9428 (comment)
#9428 (comment)
If you disagree with me, I can just leave this issue open for a while for others to chime in, or you can create a PR to make your proposal more concrete.
radarhere commentedon Oct 10, 2026
Closing, as no one else has chimed in. #2663 still exists, and your PR still exists as a concrete example of your suggestion.