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()的功能應該和我一樣,至少這就是我的想法。圖像未被讀取,'NoneType沒有屬性讀取'
這裏是我的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)
這將是真棒,如果我能在正確的方向推。 非常感謝。
'如果URL == '':'什麼也不做趕上'None'。一種方法是將其更改爲「if not url:',這可以阻止您的錯誤。 'url = request.get('url')'''url = None'不是''''如果找不到密鑰。 – roganjosh
@roganjosh - 感謝您的回覆。我只是試圖做到這一點,但同樣的錯誤 – SR1
所以'image_original = io.imread(url)'之前,你從'print(url)'得到什麼? – roganjosh