OCR & images

Transparent PNG turns black after OCR; how to properly apply a white background?

A text PNG with a transparent background looks fine on the web page, but after being fed into a certain processing pipeline, it turns into a black background. T

阅读文本 / Reading text
This paper uses synthetic samples to demonstrate the processing method. The code has been verified using examples in an isolated environment; this does not guarantee effectiveness for all real files or products. Before processing important files, please retain the original copies and verify them against the boundaries and acceptance criteria outlined in this document. A text PNG with a transparent background looks fine on the web page, but after being fed into a certain processing pipeline, it turns into a black background. The RGB values of the transparent pixels do not correspond to the final displayed color. Directly discarding the alpha channel is equivalent to ignoring the compositing process, and the result may differ significantly from what the user sees. is first synthesized, then converted to RGB If the business rules specify that OCR input must use a white background, first create an opaque white background, composite the RGBA image onto it, and then convert to RGB. Here, the white background is a clearly defined input strategy; for light-colored text, colored paper, or other visual semantics, it is still necessary to verify whether this approach applies. uses a comparison between fully transparent and opaque pixels from PIL import Image source = Image.new("RGBA", (2, 1), (0, 0, 0, 0)) source.putpixel((1, 0), (0, 0, 0, 255)) wrong = source.convert("RGB") background = Image.new("RGBA", source.size, (255, 255, 255, 255)) correct = Image.alpha_composite(background, source).convert("RGB") assert wrong.getpixel((0, 0)) == (0, 0, 0) assert correct.getpixel((0, 0)) == (255, 255, 255) assert correct.getpixel((1, 0)) == (0, 0, 0) assert correct.mode == "RGB" assert source.mode == "RGBA" This is a composite teaching example consisting of two pixels. It illustrates the order of handling transparent backgrounds and does not validate the recognition results of any OCR engine. The actual input may also include a transparent index. P mode images typically need to be first normalized to RGBA, then processed through the same compositing steps. different modes require explicit handling of input recommends checking Common errors RGBA alpha channel and background strategy directly discards the transparency channel palette PNG Is there a transparent index? only checks whether the pattern name contains A standard RGB original background color does not need to fabricate a transparent layer translucent stroke Post-synthesis edge and contrast forced threshold causes stroke fragmentation retaining the original image has practical value The white-background version should be treated as a derivative product and must not overwrite the original file. If, at a later stage, the light-gray seal becomes difficult to discern, one should revert to the original document for comparison, rather than reprocessing an already edited image. The processing checklist should document the background color, input/output mode, and dimensions. enables recognition and translation based on the page that the user actually sees to OmniDoc (product entry point) Before submitting images or scanned documents, this check can be used to eliminate discrepancies between the display and the pixels. After translation, verify the main text, seals, and numerical areas. Only when the input visual state is consistent will it be easier to determine where subsequent differences originate. Reference: Pillow Image Composition Interface 。 - 1 -
Updated 2026-10-08