我已经根据在网络和一些youtube视频上找到的参考资料编写了代码,但它似乎对我不起作用,我也不知道可能是什么问题。
import io
import requests
import pytesseract
from PIL import Image
r = requests.get("http://www.teamjimmyjoe.com/wp-content/uploads/2014/09/Classic-Best-Funny-Text-Messages-earthquake-titties.jpg",stream=True)
# print( type(response) ) # <class 'requests.models.Response'>
img = Image.open(io.BytesIO(r.content))
# print( type(img) ) # <class 'PIL.JpegImagePlugin.JpegImageFile'>
text = pytesseract.image_to_string(img)
print(text)
我得到了这个错误
File "F:\Projects\FileExtractor\untitled3.py", line 16, in <module>
img = Image.open(io.BytesIO(r.content))
File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 2943, in open
raise UnidentifiedImageError(
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000001E85C0BAA40>
请帮我解决这个问题。谢谢
发布于 2021-01-21 13:06:15
有了更深一步的想法。为什么不欺骗浏览器的标题,这看起来是有效的。
import io
import requests
import pytesseract
from PIL import Image
url = 'http://www.teamjimmyjoe.com/wp-content/uploads/2014/09/Classic-Best-Funny-Text-Messages-earthquake-titties.jpg'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
r = requests.get(url, headers=headers)
img = Image.open(io.BytesIO(r.content))
# # print( type(img) ) # <class 'PIL.JpegImagePlugin.JpegImageFile'>
text = pytesseract.image_to_string(img)
#
print(text)
响应为:
Hey! | just saw on CNN
there was an earthquake
near you. Are you ok?
| Yes! We're all fine!
What did it rate.on the titty
scale?
| Well they only jiggled a |
little bit, so probably not
that high.
HAHAHAHAHAHA | LOVE
YOU
Richter scale. My phone is |
a 12 yr old boy.
—————————r
发布于 2021-01-21 12:59:13
总是从最简单的修复开始,然后从那里开始。
import requests
# import pytesseract
# from PIL import Image
r = requests.get("http://www.teamjimmyjoe.com/wp-content/uploads/2014/09/Classic-Best-Funny-Text-Messages-earthquake-titties.jpg",stream=True)
print(r.text)
产生以下结果:
<head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>
问题是你没有下载图片,你被Mod_Security屏蔽了。您需要克服这一点,然后才能获得图像,从而获得文本。
https://stackoverflow.com/questions/65827844
复制相似问题