BmpImagePlugin save palette (in "1" mode) sets a "should probably be 0" value to 255...
What did you do?
Generated an 800x480 BMP in memory with PIL to send to a TRMNL device (see the ticket on that side at https://github.com/usetrmnl/firmware/issues/112 .) The PIL code is pretty basic:
img = Image.new("1", (w, h))
draw = ImageDraw.Draw(img)
# various draw.line/draw.text
outbuf = BytesIO()
img.save(outbuf, "BMP")
retval = bytearray(outbuf.getvalue())
The relevant bit is in BmpImagePlugin.py in _save where it does a very straightforward
if im.mode == "1":
palette = b"".join(o8(i) * 4 for i in (0, 255))
The TRMNL device is looking specifically for a white and black entry, with [255,255,255,0] as black. Wikipedia suggests that that fourth byte Must Be Zero - except in specific cases where it's interpreted as alpha, that are unusual enough that while you do support it per https://github.com/python-pillow/Pillow/issues/8594 it's only with a magic flag set, suggesting it's not that common.
What did you expect to happen?
I'm not an expert in this format, just trying to intermediate - but my interpretation of at least the wikipedia summary of various references is that the relevant entry at least probably should be zero.
What actually happened?
The eInk device displayed an BMP_COLOR_SCHEME_FAILED message instead of rendering the image. (Making it more generous is the subject of the other ticket - I'm just bringing up that this might also be incorrect in this encoder.
Also, I worked around the issue with a somewhat brutal
# should be 0*4, 255*3+0, but BmpImagePlugin.py uses 0*4, 255*4
retval[61] = 0
after the code quoted above, and the device rendered the image just fine - so this is just a very narrow point of disagreement.
What are your OS, Python and Pillow versions?
- OS: ubuntu 24.04
- Python: 3.12.3
- Pillow:
python3-pil10.2.0-1ubuntu1 (but the code quoted above fromBmpImagePlugin.pyis unchanged on the primary branch.)
(For completeness: the TRMNL was running firmware 1.4.8 but that also hasn't changed on the primary branch at this time.)
Please paste here the output of running:
python3 -m PIL.report
--------------------------------------------------------------------
Pillow 10.2.0
Python 3.12.3 (main, Feb 4 2025, 14:48:35) [GCC 13.3.0]
--------------------------------------------------------------------
Python modules loaded from /usr/lib/python3/dist-packages/PIL
Binary modules loaded from /usr/lib/python3/dist-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 10.2.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.14
--- WEBP support ok, loaded 1.3.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 2.1.5
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.3
--- LIBTIFF support ok, loaded 4.5.1
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1
--- LIBIMAGEQUANT (Quantization method) support ok, loaded 2.18.0
--- XCB (X protocol) support ok
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open, save, encode
--------------------------------------------------------------------
BMP image/bmp
Extensions: .bmp
Features: open, save
--------------------------------------------------------------------
BUFR
Extensions: .bufr
Features: open, save
--------------------------------------------------------------------
CUR
Extensions: .cur
Features: open
--------------------------------------------------------------------
DCX
Extensions: .dcx
Features: open
--------------------------------------------------------------------
DDS
Extensions: .dds
Features: open, save
--------------------------------------------------------------------
DIB image/bmp
Extensions: .dib
Features: open, save
--------------------------------------------------------------------
EPS application/postscript
Extensions: .eps, .ps
Features: open, save
--------------------------------------------------------------------
FITS
Extensions: .fit, .fits
Features: open
--------------------------------------------------------------------
FLI
Extensions: .flc, .fli
Features: open
--------------------------------------------------------------------
FPX
Extensions: .fpx
Features: open
--------------------------------------------------------------------
FTEX
Extensions: .ftc, .ftu
Features: open
--------------------------------------------------------------------
GBR
Extensions: .gbr
Features: open
--------------------------------------------------------------------
GIF image/gif
Extensions: .gif
Features: open, save, save_all
--------------------------------------------------------------------
GRIB
Extensions: .grib
Features: open, save
--------------------------------------------------------------------
HDF5
Extensions: .h5, .hdf
Features: open, save
--------------------------------------------------------------------
ICNS image/icns
Extensions: .icns
Features: open, save
--------------------------------------------------------------------
ICO image/x-icon
Extensions: .ico
Features: open, save
--------------------------------------------------------------------
IM
Extensions: .im
Features: open, save
--------------------------------------------------------------------
IMT
Features: open
--------------------------------------------------------------------
IPTC
Extensions: .iim
Features: open
--------------------------------------------------------------------
JPEG image/jpeg
Extensions: .jfif, .jpe, .jpeg, .jpg
Features: open, save
--------------------------------------------------------------------
JPEG2000 image/jp2
Extensions: .j2c, .j2k, .jp2, .jpc, .jpf, .jpx
Features: open, save
--------------------------------------------------------------------
MCIDAS
Features: open
--------------------------------------------------------------------
MIC
Extensions: .mic
Features: open
--------------------------------------------------------------------
MPEG video/mpeg
Extensions: .mpeg, .mpg
Features: open
--------------------------------------------------------------------
MSP
Extensions: .msp
Features: open, save, decode
--------------------------------------------------------------------
PCD
Extensions: .pcd
Features: open
--------------------------------------------------------------------
PCX image/x-pcx
Extensions: .pcx
Features: open, save
--------------------------------------------------------------------
PIXAR
Extensions: .pxr
Features: open
--------------------------------------------------------------------
PNG image/png
Extensions: .apng, .png
Features: open, save, save_all
--------------------------------------------------------------------
PPM image/x-portable-anymap
Extensions: .pbm, .pgm, .pnm, .ppm
Features: open, save
--------------------------------------------------------------------
PSD image/vnd.adobe.photoshop
Extensions: .psd
Features: open
--------------------------------------------------------------------
QOI
Extensions: .qoi
Features: open
--------------------------------------------------------------------
SGI image/sgi
Extensions: .bw, .rgb, .rgba, .sgi
Features: open, save
--------------------------------------------------------------------
SPIDER
Features: open, save
--------------------------------------------------------------------
SUN
Extensions: .ras
Features: open
--------------------------------------------------------------------
TGA image/x-tga
Extensions: .icb, .tga, .vda, .vst
Features: open, save
--------------------------------------------------------------------
TIFF image/tiff
Extensions: .tif, .tiff
Features: open, save, save_all
--------------------------------------------------------------------
WEBP image/webp
Extensions: .webp
Features: open, save, save_all
--------------------------------------------------------------------
WMF
Extensions: .emf, .wmf
Features: open, save
--------------------------------------------------------------------
XBM image/xbm
Extensions: .xbm
Features: open, save
--------------------------------------------------------------------
XPM image/xpm
Extensions: .xpm
Features: open
--------------------------------------------------------------------
XVTHUMB
Features: open
--------------------------------------------------------------------
Sure. I've created https://github.com/python-pillow/Pillow/pull/8889.
If you would like to test it and need a prebuilt wheel to do so, just let us know.