Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: encode transient PNGs at zlib level 1 (#15579)

Daniel Hanb238d7007476
diff --git a/studio/backend/core/inference/diffusion_segment.py b/studio/backend/core/inference/diffusion_segment.py
index 5112e860e51..3ed07a7e873 100644
--- a/studio/backend/core/inference/diffusion_segment.py
+++ b/studio/backend/core/inference/diffusion_segment.py
@@ -1031,7 +1031,8 @@ def mask_to_png(mask: Any) -> bytes:
array = (mask.to("cpu").numpy().astype("uint8")) * 255
buffer = io.BytesIO()
- Image.fromarray(array, mode = "L").save(buffer, format = "PNG", optimize = True)
+ # Every zlib level is lossless; optimize=True only buys a few KB for several times the encode time.
+ Image.fromarray(array, mode = "L").save(buffer, format = "PNG", compress_level = 1)
return buffer.getvalue()
diff --git a/studio/backend/core/inference/sd_cpp_backend.py b/studio/backend/core/inference/sd_cpp_backend.py
index 7c70b2317c0..34b1f3b0ce2 100644
--- a/studio/backend/core/inference/sd_cpp_backend.py
+++ b/studio/backend/core/inference/sd_cpp_backend.py
@@ -2438,7 +2438,8 @@ def _native_condition_images(
canvas.paste(img, ((canvas.width - iw) // 2, (canvas.height - ih) // 2))
img = canvas
buf = io.BytesIO()
- img.save(buf, format = "PNG")
+ # Transport only (sd.cpp decodes it back): zlib level 1 is lossless and faster than Pillow's default.
+ img.save(buf, format = "PNG", compress_level = 1)
blobs.append(buf.getvalue())
return int(width), int(height), blobs
diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.py
index df387ec92c4..5f918ce8d4c 100644
--- a/studio/backend/core/inference/video.py
+++ b/studio/backend/core/inference/video.py
@@ -13165,7 +13165,7 @@ class VideoBackend:
if image is None:
return None
path = Path(scratch) / f"{name}.png"
- image.save(path, format = "PNG")
+ image.save(path, format = "PNG", compress_level = 1)
return str(path)
init_img = stage(first_frame, "first")
diff --git a/studio/backend/core/inference/video_gallery.py b/studio/backend/core/inference/video_gallery.py
index 1d3ac86dae6..9e740c3728a 100644
--- a/studio/backend/core/inference/video_gallery.py
+++ b/studio/backend/core/inference/video_gallery.py
@@ -321,6 +321,7 @@ def frame_png(
if max_px:
image.thumbnail((max_px, max_px), Image.LANCZOS)
buf = io.BytesIO()
+ # Pillow's default level on purpose: MCP callers gate on these bytes (INLINE_CAP, MAX_IMAGE_BYTES).
image.save(buf, format = "PNG")
return buf.getvalue()
diff --git a/studio/backend/core/inference/video_minimax_h3.py b/studio/backend/core/inference/video_minimax_h3.py
index ec18486416d..2f0725f6bc5 100644
--- a/studio/backend/core/inference/video_minimax_h3.py
+++ b/studio/backend/core/inference/video_minimax_h3.py
@@ -992,7 +992,8 @@ def stage_h3_references(
images: list[str] = []
for index, image in enumerate(references.images):
path = scratch / f"ref-image-{index:02d}.png"
- image.save(path, format = "PNG")
+ # Scratch files sd-cli decodes once: zlib level 1 is lossless and faster than Pillow's default.
+ image.save(path, format = "PNG", compress_level = 1)
images.append(str(path))
videos: list[str] = []
@@ -1002,7 +1003,7 @@ def stage_h3_references(
directory = scratch / f"ref-video-{index:02d}"
directory.mkdir()
for frame_index, frame in enumerate(frames):
- frame.save(directory / f"{frame_index:05d}.png", format = "PNG")
+ frame.save(directory / f"{frame_index:05d}.png", format = "PNG", compress_level = 1)
videos.append(str(directory))
if waveform is None:
# Positional pairing cannot express "skip this one", so stop pairing here.
diff --git a/studio/backend/tests/test_video_gallery_inputs.py b/studio/backend/tests/test_video_gallery_inputs.py
index 33d11cef155..709c6e4577f 100644
--- a/studio/backend/tests/test_video_gallery_inputs.py
+++ b/studio/backend/tests/test_video_gallery_inputs.py
@@ -144,3 +144,20 @@ def test_a_silent_video_has_no_audio_track_to_extract():
assert response.status_code == 400
assert response.json()["detail"].startswith("This video has no audio track")
assert list(run_as(ALICE, audio_inputs.inputs_dir).iterdir()) == []
+
+
+def test_a_frame_png_keeps_pillows_default_encoding(monkeypatch, tmp_path):
+ # MCP gates on these bytes (INLINE_CAP, MAX_IMAGE_BYTES): a larger encoding could flip an inline preview to a link.
+ from core.inference import image_gallery
+
+ av = pytest.importorskip("av")
+ path = tmp_path / "clip.mp4"
+ path.write_bytes(_real_mp4_bytes(frames = 4, size = (64, 48), rate = 4))
+ monkeypatch.delenv(image_gallery.PNG_COMPRESS_LEVEL_ENV, raising = False)
+ with av.open(str(path)) as src:
+ image = next(src.decode(src.streams.video[0])).to_image()
+ expected = io.BytesIO()
+ image.save(expected, format = "PNG")
+ assert video_gallery.frame_png(path, "first") == expected.getvalue()
+ monkeypatch.setenv(image_gallery.PNG_COMPRESS_LEVEL_ENV, "1")
+ assert video_gallery.frame_png(path, "first") == expected.getvalue()