Skip to content

Commit

Permalink
Fix for PIL version 10
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
davidplowman committed Oct 27, 2023
1 parent 128630d commit ecc168b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions picamera2/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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")
Expand Down

0 comments on commit ecc168b

Please sign in to comment.