-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add jpeg_quality
parameter to log_image
#2418
Merged
+69
−27
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ff743c2
Show if an image is JPEG-compressed in the UI
emilk ae378ac
Add jpeg_quality parameter to log_image, requiring cv2
emilk a95e8e8
Fix BGR/RGB shenanigans
emilk 82c332a
Better docstring
emilk fa81775
Less spam when running arkitscenes
emilk 7dd7202
Turn on jpeg-encoding for a bunch of our examples
emilk ea3f6cf
py-format
emilk 2b1bc1a
Use pillow/PIL instead of cv2
emilk a1534a1
Fix warning when running arkitscenes
emilk b5d0f1f
Fix the pillow version
emilk 8aaa3b3
Try another pillow version
emilk 5ac2759
Try another pillow version
emilk 08782a4
remove try-catch block
emilk 681e803
Handle any dtype
emilk a0c5194
fix pillow version snafu
emilk 9e8cc6e
py-format
emilk 151fe3b
Update .github/workflows/reusable_build_and_test_wheels.yml
emilk 0410513
Merge branch 'main' into emilk/log_image-jpeg-encode
emilk 04dbb44
Merge branch 'main' into emilk/log_image-jpeg-encode
emilk 194b4be
Maybe yaml doesn't support commas in strings. who the hell knows
emilk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
from __future__ import annotations | ||
|
||
from io import BytesIO | ||
from typing import Any | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
from PIL import Image | ||
|
||
from rerun import bindings | ||
from rerun.log.error_utils import _send_warning | ||
from rerun.log.file import ImageFormat, log_image_file | ||
from rerun.log.log_decorator import log_decorator | ||
from rerun.log.tensor import Tensor, _log_tensor, _to_numpy | ||
from rerun.recording_stream import RecordingStream | ||
|
@@ -27,6 +30,7 @@ def log_image( | |
ext: dict[str, Any] | None = None, | ||
timeless: bool = False, | ||
recording: RecordingStream | None = None, | ||
jpeg_quality: int | None = None, | ||
) -> None: | ||
""" | ||
Log a gray or color image. | ||
|
@@ -59,6 +63,14 @@ def log_image( | |
Specifies the [`rerun.RecordingStream`][] to use. | ||
If left unspecified, defaults to the current active data recording, if there is one. | ||
See also: [`rerun.init`][], [`rerun.set_global_data_recording`][]. | ||
jpeg_quality: | ||
If set, encode the image as a JPEG to save storage space. | ||
Higher quality = larger file size. | ||
A quality of 95 still saves a lot of space, but is visually very similar. | ||
JPEG compression works best for photographs. | ||
Only RGB images are supported. | ||
Note that compressing to JPEG costs a bit of CPU time, both when logging | ||
and later when viewing them. | ||
|
||
""" | ||
|
||
|
@@ -90,6 +102,23 @@ def log_image( | |
if interpretable_as_image and num_non_empty_dims != len(shape): | ||
image = np.squeeze(image) | ||
|
||
if jpeg_quality is not None: | ||
# TODO(emilk): encode JPEG in background thread instead | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it might be nice to do this in rust instead in the future to make it more cross-platform and to introduce less python dependencies There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree |
||
|
||
if image.dtype not in ["uint8", "sint32", "float32"]: | ||
# Convert to a format supported by Image.fromarray | ||
image = image.astype("float32") | ||
|
||
pil_image = Image.fromarray(image) | ||
output = BytesIO() | ||
pil_image.save(output, format="JPEG", quality=jpeg_quality) | ||
jpeg_bytes = output.getvalue() | ||
output.close() | ||
|
||
# TODO(emilk): pass draw_order too | ||
log_image_file(entity_path=entity_path, img_bytes=jpeg_bytes, img_format=ImageFormat.JPEG, timeless=timeless) | ||
return | ||
|
||
_log_tensor(entity_path, image, draw_order=draw_order, ext=ext, timeless=timeless, recording=recording) | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a BGR vs RGB bug