圖片灰度化報錯:PIL.UnidentifiedImageError: cannot identify image file
一、現象
(一)第一個報錯
import pytesseract#tesserocr 變成了pytesseract
from PIL import Image
images = Image.open('驗證.png')
images = images.convert('L')
print(pytesseract.image_to_string(images))
#image_to_text(images)#已經沒有了
'''驗證失敗'''
#灰度化
images.show()
#print(pytesseract.image_to_string(image))
#二值化
threshold = 40
table =[]
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
image = images.point(table,'1')
result = pytesseract.image_to_string(image)
print('ok')
print(result)
驗證時出現路徑錯誤:
原報錯就是此處,進入這個界面需要最後一個報錯路徑處打開
進入此處
報錯地點在這裏:報錯時位置並不是這樣(此處已經更改完成了)
(二)第二個報錯
報錯
PIL.UnidentifiedImageError: cannot identify image file 'J:\\PyCharm項目\\項目\\項目二_文書內容提取\\提取模板模塊\\parrot_new.png'
二、解決過程
第一個報錯解決
更新路徑即找到路徑
將路徑更新爲現在路徑即可
第二個報錯解決過程
更新pillow
更換另一個圖片後可以說明可能是該圖片或者是其路徑有問題
更換路徑後仍然一樣:
重新對圖片進行檢查發現,圖片錯誤
只能對圖片進行更新!!!!!!!
重新下載後
from PIL import Image
image_path ='J:\PyCharm項目\項目\項目二_文書內容提取\輸出模塊\parrot_new.png'
image = Image.open(image_path)
im_gray = image.convert('L')
im_gray.show()
成功