From ecc168b8797c70acf6fb36cecc4b94dc90e440ee Mon Sep 17 00:00:00 2001 From: David Plowman Date: Thu, 26 Oct 2023 13:18:10 +0100 Subject: [PATCH] Fix for PIL version 10 You can no longer do "img.mode = 'RGBX'" but must use img.convert instead. This seems to work on older PIL versions, but because it (presumably?) copies the image we'll arrange it so that we only need it when saving a PNG, so as not to slow up JPEG save. Signed-off-by: David Plowman --- picamera2/request.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/picamera2/request.py b/picamera2/request.py index 12c7e213..78692797 100644 --- a/picamera2/request.py +++ b/picamera2/request.py @@ -228,7 +228,7 @@ def make_image(self, buffer, config, width=None, height=None): return Image.open(io.BytesIO(buffer)) else: rgb = self.make_array(buffer, config) - mode_lookup = {"RGB888": "BGR", "BGR888": "RGB", "XBGR8888": "RGBA", "XRGB8888": "BGRX"} + mode_lookup = {"RGB888": "BGR", "BGR888": "RGB", "XBGR8888": "RGBX", "XRGB8888": "BGRX"} if fmt not in mode_lookup: raise RuntimeError(f"Stream format {fmt} not supported for PIL images") mode = mode_lookup[fmt] @@ -255,11 +255,11 @@ def save(self, img, metadata, file_output, format=None): format_str = file_output.suffix.lower() else: raise RuntimeError("Cannot determine format to save") + if format_str in ('png') and img.mode == 'RGBX': + # It seems we can't save an RGBX png file, so make it RGBA instead. We can't use RGBA + # everywhere, because we can only save an RGBX jpeg, not an RGBA one. + img = img.convert(mode='RGBA') if format_str in ('jpg', 'jpeg'): - if img.mode == "RGBA": - # Nasty hack. Qt doesn't understand RGBX so we have to use RGBA. But saving a JPEG - # doesn't like RGBA to we have to bodge that to RGBX. - img.mode = "RGBX" # Make up some extra EXIF data. if "AnalogueGain" in metadata and "DigitalGain" in metadata: datetime_now = datetime.now().strftime("%Y:%m:%d %H:%M:%S")