Skip to content

fix: support grayscale images in fit_image padding#129

Open
cryptomilk wants to merge 1 commit into
OpenDisplay:mainfrom
cryptomilk:asn-fix
Open

fix: support grayscale images in fit_image padding#129
cryptomilk wants to merge 1 commit into
OpenDisplay:mainfrom
cryptomilk:asn-fix

Conversation

@cryptomilk

Copy link
Copy Markdown

ImageOps.pad and Image.new require a color matching the image's band count. This fixes fitting for single-band (e.g. grayscale "L") source images.

I'm working on https://github.com/cryptomilk/hass-eink-dashboard which creates grayscale images, it failed with a TypeError:

Jul 12 15:05:01 antares.milli.ways hass[1429759]:   File "/usr/lib/python3.14/site-packages/homeassistant/components/opendisplay/services.py", line 225, in _async_upload_image                   
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     await device.upload_image(                                                                                                                  
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     ...<6 lines>...                                                                                                                             
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     )                                                                                                                                           Jul 12 15:05:01 antares.milli.ways hass[1429759]:   File "/usr/lib/python3.14/site-packages/opendisplay/device.py", line 979, in upload_image                                                     
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     image_data, compressed_data, processed_image = self._prepare_image(                                                                         Jul 12 15:05:01 antares.milli.ways hass[1429759]:                                                    ~~~~~~~~~~~~~~~~~~~^                                                                         
Jul 12 15:05:01 antares.milli.ways hass[1429759]:         image,                                                                                                                                  Jul 12 15:05:01 antares.milli.ways hass[1429759]:         ^^^^^^                                                                                                                                  
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     ...<10 lines>...                                                                                                                            Jul 12 15:05:01 antares.milli.ways hass[1429759]:         rotate=rotate,                                                                                                                          
Jul 12 15:05:01 antares.milli.ways hass[1429759]:         ^^^^^^^^^^^^^^                                                                                                                          Jul 12 15:05:01 antares.milli.ways hass[1429759]:     )                                                                                                                                           
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     ^                                                                                                                                           
Jul 12 15:05:01 antares.milli.ways hass[1429759]:   File "/usr/lib/python3.14/site-packages/opendisplay/device.py", line 895, in _prepare_image                                                   
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     return prepare_image(                                                                                                                       
Jul 12 15:05:01 antares.milli.ways hass[1429759]:         image,                                                                                                                                  
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     ...<14 lines>...                                                                                                                            
Jul 12 15:05:01 antares.milli.ways hass[1429759]:         rotate=rotate,                                                                                                                          
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     )                                                                                                                                           
Jul 12 15:05:01 antares.milli.ways hass[1429759]:   File "/usr/lib/python3.14/site-packages/opendisplay/device.py", line 209, in prepare_image                                                    
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     image = fit_image(image, target_size, fit)                                                                                                  
Jul 12 15:05:01 antares.milli.ways hass[1429759]:   File "/usr/lib/python3.14/site-packages/opendisplay/encoding/images.py", line 38, in fit_image                                                
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     return ImageOps.pad(image, target_size, Image.Resampling.LANCZOS, color=_PAD_COLOR)                                                         
Jul 12 15:05:01 antares.milli.ways hass[1429759]:            ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jul 12 15:05:01 antares.milli.ways hass[1429759]:   File "/usr/lib64/python3.14/site-packages/PIL/ImageOps.py", line 363, in pad
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     out = Image.new(image.mode, size, color)
Jul 12 15:05:01 antares.milli.ways hass[1429759]:   File "/usr/lib64/python3.14/site-packages/PIL/Image.py", line 3241, in new
Jul 12 15:05:01 antares.milli.ways hass[1429759]:     return im._new(core.fill(mode, size, color))
Jul 12 15:05:01 antares.milli.ways hass[1429759]:                    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^
Jul 12 15:05:01 antares.milli.ways hass[1429759]: TypeError: color must be int or single-element tuple

ImageOps.pad and Image.new require a color matching the image's band
count. This fixes fitting for single-band (e.g. grayscale "L") source
images.

Tests have been written by Claude.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@cryptomilk cryptomilk requested a review from g4bri3lDev as a code owner July 12, 2026 15:36
@cryptomilk cryptomilk marked this pull request as draft July 12, 2026 15:36
@cryptomilk

Copy link
Copy Markdown
Author

Tested and works.

@cryptomilk cryptomilk marked this pull request as ready for review July 12, 2026 21:05
@codecov

codecov Bot commented Jul 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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 this pull request may close these issues.

1 participant