import pytesseract
import requests
from io import BytesIO
from PIL import Image,ImageOps
image_url = 'gif'
def optimize_and_ocr_from_url(image_url, tesseract_config="--psm 6"):
# Download the image from the URL
response = requests.get(image_url)
image = Image.open(BytesIO(response.content))
# Optimize the image (resize, convert to grayscale, etc.)
optimized_image = optimize_image(image)
# Use Tesseract OCR to extract text from the optimized image
text = pytesseract.image_to_string(optimized_image, config=tesseract_config)
# Print the OCR result to the console
print("OCR Result:")
print(text)
def optimize_image(image, target_resolution=(109, 50)):
# Resize the image to the target resolution
resized_image = image.resize(target_resolution)
# Convert the image to grayscale
grayscale_image = ImageOps.grayscale(resized_image)
return grayscale_image
# Example usage
optimize_and_ocr_from_url(image_url)
我尝试灰度化,尝试使用 tesseract_config 但它似乎不起作用。只有空响应。我在 stackoverflow 上尝试了其他解决方案,但无法做到这一点。 我用的是windows
我尝试在
gif
/tesseract
/pytesseract
(在 Linux 上)中直接使用您的 console
和 terminal
(而不是 bash
)
$ tesseract input.gif -
Warning: Invalid resolution 0 dpi. Using 70 instead.
Estimating resolution as 270
Empty page!!
Estimating resolution as 270
Empty page!!
接下来我尝试用
convert
来imagemagick
它,看起来它没有什么问题:
dpi
的信息添加到文件中,但似乎 gif
无法保留它,所以我不得不转换为 png
。200x24
tesseract
不需要它,但当我在任何图像查看器中检查它时它更具可读性)$ convert input.gif -resize 200x24 -units pixelsperinch -density 72 -background white -flatten output.png
output.png
现在它可以识别文本了,但还有另一个问题:
没有
top margin
,因此无法识别 7
- 它检测到 /
+3/0 3/7 312208
所以我用
-gravity south -extent 200x28
来添加边距
$ convert input.gif -resize 200x24 -units pixelsperinch -density 72 -background white -flatten -gravity south -extent 200x28 output-with-margin.png
现在
tesseract
可以正确检测数字。
output-with-margin.png
(Stackoverflow
有白色背景,所以你看不到新的边距)
您可以尝试对
Pillow
执行相同的操作,或者您可以使用 subprocess.run()
直接运行 imagemagick 或者您可以尝试使用使用 imagemagick
的 Python 模块Wand