Skip to content

Commit ae1d898

Browse files
fix(python): convert palette and CMYK images before OCR (#773)
img_to_ndarray passed the raw PIL array straight to convert_img. For palette (P) images that array holds palette indices, so a typical two-colour PNG reaches the detector as an almost black image and no text is found. CMYK images give four ink channels that were treated as RGBA, so a CMYK JPEG also returned no result. Convert P images to RGBA, which keeps any palette transparency for the existing alpha handling, and CMYK images to RGB. Path, bytes and PIL inputs all go through this function.
1 parent 1b9573d commit ae1d898

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎python/rapidocr/utils/load_image.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def img_to_ndarray(self, img: Image.Image) -> np.ndarray:
124124
if img.mode == "1":
125125
img = img.convert("L")
126126
return np.array(img)
127+
128+
# The raw arrays of these modes hold palette indices or ink values,
129+
# not the colours later steps expect.
130+
if img.mode == "P":
131+
img = img.convert("RGBA")
132+
elif img.mode == "CMYK":
133+
img = img.convert("RGB")
127134
return np.array(img)
128135

129136
def convert_img(self, img: np.ndarray, origin_img_type: Any) -> np.ndarray:

‎python/tests/test_input.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import cv2
99
import numpy as np
1010
import pytest
11+
from PIL import Image
1112
from pytest import mark
1213

1314
root_dir = Path(__file__).resolve().parent.parent
@@ -314,6 +315,59 @@ def test_mode_one_img(engine):
314315
assert result.txts[0] == "TEST"
315316

316317

318+
@pytest.mark.parametrize("input_type", ["path", "bytes", "pil"])
319+
def test_palette_img_loads_palette_colors(tmp_path, input_type):
320+
img = Image.new("P", (3, 1))
321+
img.putpalette([255, 255, 255, 0, 0, 0, 200, 30, 10])
322+
img.putdata([0, 1, 2])
323+
path = tmp_path / "palette.png"
324+
img.save(path)
325+
inputs = {"path": path, "bytes": path.read_bytes(), "pil": img}
326+
327+
result = LoadImage()(inputs[input_type])
328+
329+
expected = np.array([[[255, 255, 255], [0, 0, 0], [10, 30, 200]]], dtype=np.uint8)
330+
np.testing.assert_array_equal(result, expected)
331+
332+
333+
def test_palette_img_with_transparency_uses_alpha():
334+
img = Image.new("P", (2, 1))
335+
img.putpalette([0, 0, 0, 0, 0, 0])
336+
img.putdata([0, 1])
337+
img.info["transparency"] = 0
338+
339+
result = LoadImage()(img)
340+
341+
expected = np.array([[[255, 255, 255], [0, 0, 0]]], dtype=np.uint8)
342+
np.testing.assert_array_equal(result, expected)
343+
344+
345+
@pytest.mark.parametrize("input_type", ["path", "bytes", "pil"])
346+
def test_cmyk_img_loads_as_bgr(tmp_path, input_type):
347+
img = Image.new("CMYK", (3, 1))
348+
img.putdata([(0, 0, 0, 0), (0, 0, 0, 255), (0, 255, 255, 0)])
349+
path = tmp_path / "cmyk.tif"
350+
img.save(path)
351+
inputs = {"path": path, "bytes": path.read_bytes(), "pil": img}
352+
353+
result = LoadImage()(inputs[input_type])
354+
355+
expected = np.array([[[255, 255, 255], [0, 0, 0], [0, 0, 255]]], dtype=np.uint8)
356+
np.testing.assert_array_equal(result, expected)
357+
358+
359+
def test_palette_img(engine, tmp_path):
360+
gray = cv2.imread(str(test_dir / "en.jpg"), cv2.IMREAD_GRAYSCALE)
361+
img = Image.new("P", (gray.shape[1], gray.shape[0]))
362+
img.putpalette([255, 255, 255, 0, 0, 0])
363+
img.putdata((gray < 128).astype(np.uint8).flatten().tolist())
364+
path = tmp_path / "palette.png"
365+
img.save(path)
366+
367+
result = engine(path)
368+
assert len(result) > 0
369+
370+
317371
@mark.parametrize(
318372
"img_name,gt_len,gt_first_len",
319373
[

0 commit comments

Comments
 (0)