Rembg is a tool to remove images background
fix: convert non-RGB images before uploading in remote sessions (#859)
atatogan09921ced96df
diff --git a/rembg/sessions/atlascloud.py b/rembg/sessions/atlascloud.pyindex ccca972..150baff 100644--- a/rembg/sessions/atlascloud.py+++ b/rembg/sessions/atlascloud.py@@ -113,6 +113,10 @@ class AtlasCloudSession(BaseSession):RuntimeError: If the API errors, the prediction fails, or it doesnot finish within the polling timeout."""+ # PNG cannot store every PIL mode (e.g. CMYK JPEGs), so normalize first.+ if img.mode not in ("RGB", "RGBA"):+ img = img.convert("RGBA" if img.has_transparency_data else "RGB")+buf = io.BytesIO()img.save(buf, format="PNG")image_bytes = buf.getvalue()diff --git a/rembg/sessions/withoutbg.py b/rembg/sessions/withoutbg.pyindex 07c6ac5..2433813 100644--- a/rembg/sessions/withoutbg.py+++ b/rembg/sessions/withoutbg.py@@ -72,6 +72,10 @@ class WithoutBgSession(BaseSession):ValueError: If the PNG-encoded image exceeds the 20 MB upload limit.RuntimeError: If the API returns a non-200 response."""+ # PNG cannot store every PIL mode (e.g. CMYK JPEGs), so normalize first.+ if img.mode not in ("RGB", "RGBA"):+ img = img.convert("RGBA" if img.has_transparency_data else "RGB")+buf = io.BytesIO()img.save(buf, format="PNG")image_bytes = buf.getvalue()diff --git a/tests/test_atlascloud.py b/tests/test_atlascloud.pyindex 6dcff57..9bbb528 100644--- a/tests/test_atlascloud.py+++ b/tests/test_atlascloud.py@@ -1,3 +1,4 @@+import base64import ioimport jsonfrom unittest.mock import MagicMock, patch@@ -125,6 +126,32 @@ def test_predict_polls_then_returns_mask_matching_input_size(monkeypatch):assert body["image"].startswith("data:image/png;base64,")+def test_predict_accepts_cmyk_input(monkeypatch):+ # CMYK JPEGs are common, but PNG cannot store CMYK, so the upload+ # must convert the image instead of crashing in img.save().+ monkeypatch.setenv("ATLASCLOUD_API_KEY", "apikey_test")+ session = AtlasCloudSession("atlascloud", None)++ img = Image.new("CMYK", (40, 30), (0, 255, 255, 0))+ cutout_bytes = _png_bytes("RGBA", size=(40, 30), color=(1, 2, 3, 200))++ responses = [+ _json_resp({"data": {"id": "req-1"}}),+ _json_resp({"data": {"status": "completed", "outputs": ["https://cdn/x.png"]}}),+ _bytes_resp(cutout_bytes),+ ]++ with patch(+ "rembg.sessions.atlascloud.request.urlopen", side_effect=responses+ ) as mock_urlopen:+ masks = session.predict(img)++ assert masks[0].size == img.size+ body = json.loads(mock_urlopen.call_args_list[0][0][0].data.decode("utf-8"))+ uploaded = Image.open(io.BytesIO(base64.b64decode(body["image"].split(",", 1)[1])))+ assert uploaded.mode == "RGB"++def test_predict_raises_when_prediction_fails(monkeypatch):monkeypatch.setenv("ATLASCLOUD_API_KEY", "apikey_test")monkeypatch.setattr("rembg.sessions.atlascloud.POLL_INTERVAL", 0)diff --git a/tests/test_withoutbg.py b/tests/test_withoutbg.pyindex a944bd0..c4523a0 100644--- a/tests/test_withoutbg.py+++ b/tests/test_withoutbg.py@@ -90,6 +90,28 @@ def test_predict_returns_mask_matching_input_size(monkeypatch):assert b'name="file"' in req.data+def test_predict_accepts_cmyk_input(monkeypatch):+ # CMYK JPEGs are common, but PNG cannot store CMYK, so the upload+ # must convert the image instead of crashing in img.save().+ monkeypatch.setenv("WITHOUTBG_API_KEY", "sk_test")+ session = WithoutBgSession("withoutbg", None)++ img = Image.new("CMYK", (40, 30), (0, 255, 255, 0))++ mock_resp = MagicMock()+ mock_resp.read.return_value = _png_bytes("L", size=(40, 30), color=200)+ mock_resp.__enter__.return_value = mock_resp+ mock_resp.__exit__.return_value = False++ with patch(+ "rembg.sessions.withoutbg.request.urlopen", return_value=mock_resp+ ) as mock_urlopen:+ masks = session.predict(img)++ assert masks[0].size == img.size+ assert b"\x89PNG" in mock_urlopen.call_args[0][0].data++def test_predict_http_error_raises(monkeypatch):monkeypatch.setenv("WITHOUTBG_API_KEY", "sk_test")session = WithoutBgSession("withoutbg", None)