Skip to content

Commit

Permalink
python sdk updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Aug 16, 2024
1 parent 26dcbf4 commit 47edbf6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion rerun_notebook/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions rerun_py/rerun_sdk/rerun/archetypes/image_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(
`1x480x640x3x1` is treated as a `480x640x3`.
You also need to specify the `color_model` of it (e.g. "RGB").
color_model:
L, RGB, RGBA, etc, specifying how to interpret `image`.
L, RGB, RGBA, BGR, BGRA, etc, specifying how to interpret `image`.
pixel_format:
NV12, YUV420, etc. For chroma-downsampling.
Requires `width`, `height`, and `bytes`.
Expand Down Expand Up @@ -207,9 +207,9 @@ def __init__(
if channels == 1:
color_model = ColorModel.L
elif channels == 3:
color_model = ColorModel.RGB # TODO(#2340): change default to BGR
color_model = ColorModel.RGB
elif channels == 4:
color_model = ColorModel.RGBA # TODO(#2340): change default to BGRA
color_model = ColorModel.RGBA
else:
_send_warning_or_raise(f"Expected 1, 3, or 4 channels; got {channels}")
else:
Expand Down Expand Up @@ -278,10 +278,9 @@ def compress(self: Any, jpeg_quality: int = 95) -> EncodedImage | Image:
if image_format.pixel_format is not None:
raise ValueError(f"Cannot JPEG compress an image with pixel_format {image_format.pixel_format}")

if image_format.color_model not in (ColorModel.L, ColorModel.RGB):
# TODO(#2340): BGR support!
if image_format.color_model not in (ColorModel.L, ColorModel.RGB, ColorModel.BGR):
raise ValueError(
f"Cannot JPEG compress an image of type {image_format.color_model}. Only L (monochrome) and RGB are supported."
f"Cannot JPEG compress an image of type {image_format.color_model}. Only L (monochrome), RGB and BGR are supported."
)

if image_format.channel_datatype != ChannelDatatype.U8:
Expand All @@ -308,7 +307,12 @@ def compress(self: Any, jpeg_quality: int = 95) -> EncodedImage | Image:
else:
image = buf.reshape(image_format.height, image_format.width, 3)

mode = str(image_format.color_model)
# PIL doesn't understand BGR.
if image_format.color_model == ColorModel.BGR:
mode = "RGB"
image = image[:, :, ::-1]
else:
mode = str(image_format.color_model)

pil_image = PILImage.fromarray(image, mode=mode)
output = BytesIO()
Expand Down

0 comments on commit 47edbf6

Please sign in to comment.