紫底白字图片Tesseract无法识别,如何实现正确OCR?
Got it, let's work through this OCR issue—purple-on-white color schemes can throw Tesseract off even after a basic black-and-white conversion, since it’s optimized for the more common white-background-black-text format. Here are actionable tweaks to get your text recognized:
1. Invert Colors First (Critical!)
Tesseract performs best with light backgrounds and dark text. Since your image has the opposite, flipping the colors first will align it with Tesseract's default expectations. Try this code:
from PIL import Image, ImageOps import pytesseract # Load the original image image = Image.open("question.png") # Invert colors: purple background → white, white text → black inverted_image = ImageOps.invert(image) # Convert to pure black-and-white (optional but often helpful) bw_image = inverted_image.convert('1') bw_image.save("inverted_question.png") # Save to verify the result # Run OCR with your preferred PSM setting text = pytesseract.image_to_string(bw_image, lang='eng', config='-psm 6') print(text)
2. Use Custom Thresholding Instead of Default B&W Conversion
The convert('1') method uses a fixed default threshold, which might not properly separate your purple background from white text. Manually setting a threshold based on your image's grayscale values can yield better results:
from PIL import Image import pytesseract image = Image.open("question.png") # Convert to grayscale first gray_image = image.convert('L') # Adjust the threshold value (test 100-150 if needed) threshold = 127 # Pixels brighter than threshold become white, others black custom_bw = gray_image.point(lambda x: 255 if x > threshold else 0, '1') text = pytesseract.image_to_string(custom_bw, lang='eng', config='-psm 6') print(text)
3. Experiment with Different PSM Modes
While -psm 6 assumes a single uniform block of text, your image might benefit from other page segmentation modes. Try these alternatives:
# Automatic page segmentation (good if text is split into sections) text = pytesseract.image_to_string(bw_image, lang='eng', config='-psm 3') # Sparse text detection (useful if text is scattered) text = pytesseract.image_to_string(bw_image, lang='eng', config='-psm 11')
4. Boost Contrast for Fainter Text
If inverted text still looks washed out, enhance the contrast to make text edges sharper:
from PIL import Image, ImageOps, ImageEnhance image = Image.open("question.png") inverted = ImageOps.invert(image) # Increase contrast (adjust the 2.0 value—1.0 is original, higher = more contrast) enhancer = ImageEnhance.Contrast(inverted) enhanced_image = enhancer.enhance(2.0) bw_image = enhanced_image.convert('1') text = pytesseract.image_to_string(bw_image, lang='eng', config='-psm 6') print(text)
Why Your Original Code Failed
The default convert('1') conversion uses a generic threshold that doesn’t account for your purple background’s specific color values. This can result in either the text getting merged with the background or the background not being fully stripped out—both of which confuse Tesseract. Inverting the colors first fixes this core issue.
内容的提问来源于stack exchange,提问作者Pallab

