127.0.0.1 - - [17/Aug/2017 17:39:07] "GET /hello?url=http://i.imgur.com/HYMQOl7.jpg HTTP/1.1" 500 781
https://lh4.ggpht.com/mJDgTDUOtIyHcrb69WM0cpaxFwCNW6f0VQ2ExA7dMKpMDrZ0A6ta64OCX3H-NMdRd20=w300
Traceback (most recent call last):
File "/Users/SR/anaconda2/lib/python2.7/site-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/Users/SR/anaconda2/lib/python2.7/site-packages/bottle.py", line 1740, in wrapper
rv = callback(*a, **ka)
File "helloworld.py", line 29, in expandit
txt = pytesseract.image_to_string('temp.png')
File "/Users/SR/anaconda2/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 117, in image_to_string
image.save(input_file_name)
AttributeError: 'str' object has no attribute 'save'
我真的不知道问题所在。 imread()的功能应该像我使用它一样,至少是我的想法。
这是我的Python代码:
import urllib2
import numpy as np
import pytesseract
from skimage.color import rgb2gray
from skimage import data
from skimage import io
from PIL import Image, ImageEnhance, ImageFilter
from bottle import route, run, request, get
@get('/hello')
def expandit():
url = request.get('url')
if url == '':
return {"success": "false"}
else:
image_original = io.imread(url) #creates a temp file
image = rgb2gray(image_original)
io.imsave(temp.png, image)
txt = pytesseract.image_to_string(temp.png)
return {"text": txt, "success": "true"}
run(host='localhost', port=8080, reloader=True)
如果我能被推向正确的方向,那将是非常棒的。
万分感谢。
答案 0 :(得分:1)
您收到此错误的原因是url
为None
试试这段代码:
image_original = io.imread(None)
会出现同样的错误:
AttributeError: 'NoneType' object has no attribute 'read'
如果条件:
,请更改您的代码# to test override url
url = 'https://lh4.ggpht.com/mJDgTDUOtIyHcrb69WM0cpaxFwCNW6f0VQ2ExA7dMKpMDrZ0A6ta64OCX3H-NMdRd20=w300'
if url:
image_original = io.imread(url)
# read image and convert it.
image = rgb2gray(image_original)
io.imsave('temp.png', image)
im = Image.open('temp.png')
bg = Image.new("RGB", im.size, (255,255,255))
bg.paste(im)
txt = pytesseract.image_to_string(bg)
return {"text": txt, "success": "true"}
else:
return {"success": "false"}
确保安装了tesseract-ocr
sudo apt-get install tesseract-ocr