PIL.UnidentifiedImageError:\u无法识别图像文件

2024-10-01 05:02:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,它适用于大多数类型的图像。但由于某些原因,它不适用于仅包含1页和pdf的tiff图像

我有一个错误:

回溯(最近一次呼叫最后一次): 文件“/Users/fatiatravaille/Downloads/ocr_json/test.py”,第8行,在 image=image.open(r./radio\u lomb\u 300.tiff') 打开文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site packages/PIL/Image.py”,第3023行 引发无法识别的图像错误( PIL.UnidentifiedImageError:无法识别图像文件'/radio\u lomb\u 300.tiff'

import pytesseract
try:
    from PIL import Image
except ImportError:
    import Image


image  = Image.open(r'./radio_lomb_300.tiff')


text=(pytesseract.image_to_string(image, lang='fra'))+'\n\n\n\n'
with open('text.test_ocr2','w') as fp: fp.write(text)
text=(pytesseract.image_to_boxes(image, lang='fra'))
with open('boundingBoxes.test_ocr2','w') as fp: fp.write(text)
text=(pytesseract.image_to_data(image, lang='fra'))
with open('data.test_ocr2','w') as fp: fp.write(text)
text=(pytesseract.image_to_osd(image))
with open('osd.test_ocr2','w') as fp: fp.write(text)
pdf = pytesseract.image_to_pdf_or_hocr(image, extension='pdf', lang='fra')
with open('test_ocr2.pdf', 'w+b') as f: f.write(pdf)
hocr = pytesseract.image_to_pdf_or_hocr(image, extension='hocr', lang='fra')
with open('test_ocr2.xml', 'w+b') as f: f.write(hocr)
hocr = pytesseract.image_to_pdf_or_hocr(image, extension='hocr', lang='fra')
with open('test_ocr2.xml', 'w+b') as f: f.write(hocr)
hocr = pytesseract.image_to_alto_xml(image)
with open('test_ocr_alto2.xml', 'w+b') as f: f.write(hocr)

Tags: totexttestimagelangpdfaswith
1条回答
网友
1楼 · 发布于 2024-10-01 05:02:27

您是否尝试与opencv一起使用

例如,当我使用opencv时

import cv2
import pytesseract

# Load the img
img = cv2.imread("radio_lomb_300.tiff")

# Cvt to gry
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

txt = pytesseract.image_to_string(gry, config=" psm 6")
print(txt)

结果将是:

Hirson le 06/04/2006
Mon Cher Confrére ,
Voici les clichés du rachis lombo-sacré et du bassin de face de :
Mme PINVIN Marie
Mise au point de lombalgies.
De face, le segment lombaire est pratiquement droit, de profil, la lordose est réguliére.
On note une déminéralisation osseuse diffuse.
Les vertébres sont de hauteur normale.
On note un pincement discal postérieur sur l’ensemble du segment lombaire.
Pincement discal plus important en LA-L5 et L5-S1.
En LA-L5, on note également un important pont osseux ostéophytique marginal antérieur
droit.
L’examen du bassin ne montre pas de lésion osseuse.
Les rapports articulaires sont intacts, inclinaison du bassin vers la droite d’environ 10 mm.
Avec mes remerciements, je vous prie de croire en l’expression de mes sentiments confra-
ternels les meilleurs.

Dr MICHEL HUBERTY

Examen numérisé : 7 incidences
SH

虽然我不确定输出有多准确。您可以检查page segmentation methods以提高质量

相关问题 更多 >