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: keep dark ink visible in transparent source images (#12576)

Nilay0cf8c57ca1b3
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index 4538c6f454d..986211a0436 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -160,6 +160,7 @@ from .diffusion_memory import (
)
from .diffusion_torchao_patches import install_torchao_int_mm_patch
from .image_orientation import exif_upright
+from .mcp_images import flattened_rgb
from .media_decode_phase import decode_phase
from .diffusion_speed import (
SPEED_DEFAULT,
@@ -582,7 +583,7 @@ def decode_b64_image(
raise # the size guard's own message; don't wrap it as a decode error
except Exception as exc: # noqa: BLE001 - surfaced as a 400 to the client
raise ValueError(f"Could not decode image: {exc}") from exc
- return img.convert(mode)
+ return flattened_rgb(img) if mode == "RGB" else img.convert(mode)
def _snap_to_multiple(img: Any, multiple: int = 16) -> Any:
diff --git a/studio/backend/core/inference/mcp_images.py b/studio/backend/core/inference/mcp_images.py
index cd774291b5e..58e2272e94e 100644
--- a/studio/backend/core/inference/mcp_images.py
+++ b/studio/backend/core/inference/mcp_images.py
@@ -344,6 +344,9 @@ def flattened_rgb(image):
return image.convert("RGB")
rgba = image if image.mode == "RGBA" else image.convert("RGBA")
alpha = rgba.getchannel("A")
+ # Browser canvas exports are RGBA even when opaque; skip the composite (same pixels).
+ if alpha.getextrema()[0] == 255:
+ return rgba.convert("RGB")
# Alpha-weighted: light ink (dark-mode logos, white text) goes onto black, not white.
ink = ImageStat.Stat(ImageChops.multiply(rgba.convert("L"), alpha)).sum[0]
light = 255 * ink > 128 * ImageStat.Stat(alpha).sum[0] > 0
diff --git a/studio/backend/tests/test_diffusion_transparent_source.py b/studio/backend/tests/test_diffusion_transparent_source.py
new file mode 100644
index 00000000000..d5baf8209d5
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_transparent_source.py
@@ -0,0 +1,61 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+import base64
+import io
+
+import pytest
+
+from core.inference.diffusion import decode_b64_image
+from core.inference.diffusion_controlnet import preprocess_control
+
+PIL = pytest.importorskip("PIL.Image")
+np = pytest.importorskip("numpy")
+
+
+def _data_url(img) -> str:
+ buf = io.BytesIO()
+ img.save(buf, format = "PNG")
+ return "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
+
+
+def _black_ink_on_transparent():
+ img = PIL.new("RGBA", (200, 80), (0, 0, 0, 0))
+ img.paste((0, 0, 0, 255), (60, 20, 140, 60))
+ return img
+
+
+def test_a_transparent_source_reaches_the_model_on_white():
+ img = decode_b64_image(_data_url(_black_ink_on_transparent()))
+ assert img.mode == "RGB"
+ assert img.getpixel((5, 5)) == (255, 255, 255)
+ assert img.getpixel((100, 40)) == (0, 0, 0)
+
+
+def test_transparent_line_art_still_gives_a_canny_edge_map():
+ sketch = PIL.new("RGBA", (128, 128), (0, 0, 0, 0))
+ for x in range(20, 108):
+ for y in (30, 31, 96, 97):
+ sketch.putpixel((x, y), (0, 0, 0, 255))
+ edges = preprocess_control(decode_b64_image(_data_url(sketch)), "canny")
+ assert np.asarray(edges).max() == 255
+
+
+def test_a_paletted_transparent_png_reaches_the_model_on_white():
+ img = decode_b64_image(_data_url(_black_ink_on_transparent().quantize(colors = 4)))
+ assert img.getpixel((5, 5)) == (255, 255, 255)
+ assert img.getpixel((100, 40)) == (0, 0, 0)
+
+
+def test_rgba_and_mask_decodes_keep_their_alpha_and_values():
+ data = _data_url(_black_ink_on_transparent())
+ assert decode_b64_image(data, mode = "RGBA").getpixel((5, 5)) == (0, 0, 0, 0)
+ assert decode_b64_image(data, mode = "L").getpixel((5, 5)) == 0
+
+
+def test_an_opaque_rgba_canvas_export_decodes_like_rgb():
+ img = PIL.new("RGBA", (64, 48), (10, 200, 30, 255))
+ img.paste((200, 0, 120, 255), (16, 12, 48, 36))
+ assert decode_b64_image(_data_url(img)).tobytes() == img.convert("RGB").tobytes()