2012-04-19 66 views
1

我正在使用python2.7在GAE上進行簡單的應用,此應用程序的目的是過濾用戶上傳的圖像並將此圖像存儲在GAE中的blobstore中我試圖在blobstore中存儲原始圖像(上傳一個),然後通過使用url進行獲取並對其進行過濾,最後將過濾後的圖像存儲在blobstore上,原始圖像正確存儲,但過濾的圖像沒有正確存儲。 這是我試過的代碼:在GAE中的blobstore上存儲過濾後的圖像

from __future__ import with_statement 
from google.appengine.api import files 
from PIL import Image 
from PIL import ImageFilter 
import cgi, cgitb ; cgitb.enable() 
from google.appengine.ext import blobstore 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp import blobstore_handlers 
from google.appengine.ext.webapp.util import run_wsgi_app 
import mimetypes 
from google.appengine.ext import blobstore 
from mimetypes import guess_type 
from google.appengine.api import images 


def mime_type(filename): 
    return guess_type(filename)[0] 
class get(webapp.RequestHandler): 
    def post(self): 

     form = cgi.FieldStorage() 
     file_upload = form['file'] 
     name=file_upload.filename  
     m=mimetypes.guess_type(name)[0] 
     u_file = files.blobstore.create(mime_type=m,_blobinfo_uploaded_filename=name) 
     data=file_upload.file.read() 
     with files.open(u_file, 'a') as f: 
       f.write(data) 
     files.finalize(u_file) 
     blob_key = files.blobstore.get_blob_key(u_file) 
     url = images.get_serving_url(blob_key) 
     imageFile = str(url) 
     img = images.Image(blob_key=blob_key) 
     blob_info = blobstore.BlobInfo.get(blob_key) 
     im = Image.open(blob_info.open()) 
     out = im.filter(ImageFilter.EDGE_ENHANCE_MORE) 
     u_file = files.blobstore.create(mime_type=m,_blobinfo_uploaded_filename=name) 
     data=out 
     with files.open(u_file, 'a') as f: 
       f.write(data) 
     files.finalize(u_file) 
     blob_key = files.blobstore.get_blob_key(u_file) 
     url = images.get_serving_url(blob_key) 
     self.response.out.write("""<html><br><body style="background-color:#CC9999"><b><font size="5" face="Batang" ><center> <li ><img src="%s"</a> 
       </center></font><hr></body></html>    
       """ % (str(url))) 

def main(): 
    #application = webapp.WSGIApplication([('/serve/([^/]+)?', ServeHandler),], debug=True) 
    application = webapp.WSGIApplication([(r'/get.py', get)], debug=True) 

    run_wsgi_app(application) 


if __name__ == "__main__": 
    main() 

這是日誌文件:

2012-04-19 13:36:25.073 
Traceback (most recent call last): 
    File "/base/data/home/apps/s~filterimages2012/1.358325815417263944/get.py", line 59, in <module> 
    main() 
    File "/base/data/home/apps/s~filterimages2012/1.358325815417263944/get.py", line 55, in main 
    run_wsgi_app(application) 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/webapp/util.py", line 98, in run_wsgi_app 
    run_bare_wsgi_app(add_wsgi_middleware(application)) 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/webapp/util.py", line 116, in run_bare_wsgi_app 
    result = application(env, _start_response) 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1519, in __call__ 
    response = self._internal_error(e) 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__ 
    return handler.dispatch() 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch 
    return method(*args, **kwargs) 
    File "/base/data/home/apps/s~filterimages2012/1.358325815417263944/get.py", line 43, in post 
    f.write(data) 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/files/file.py", line 316, in write 
    request.set_data(data) 
    File "cpp_message.pyx", line 124, in cpp_message.SetScalarAccessors.Setter (third_party/apphosting/python/protobuf/proto1/cpp_message.cc:2229) 
TypeError: <type 'instance'> has type <type 'instance'>, but expected one of: str, unicode 

感謝您的幫助。

回答

1

問題是這兩條線:

out = im.filter(ImageFilter.EDGE_ENHANCE_MORE) 
# ... 
data = out 

out是PIL Image對象(因此,data太),和你想直接寫一個文件。首先,您需要以您選擇的文件格式(例如PNG,JPG)序列化圖像。例如,將第二行更改爲此將有效:

buf = cStringIO.StringIO() 
out.save(buf, "PNG") 
data = buf.getvalue() 
+0

感謝尼克,它工作正常。 – 2012-04-20 08:44:49

+0

尼克我想問** out.save(buf,「PNG」)**,我可以用圖像擴展替換「PNG」嗎?你能向我解釋這是什麼意思?謝謝你的迴應。 – 2012-04-20 09:04:17

+0

@ EngDoa'a「PNG」是文件格式的名稱。 PIL支持其中的一些,包括「JPEG」。有關詳細信息,請參閱PIL文檔。 – 2012-04-22 00:27:24

0

我不確定錯誤是什麼意思,但我不認爲你可以在Python中使用'get'作爲類名。

如何嘗試改變

application = webapp.WSGIApplication([(r'/get.py', get)], debug=True) 

application = webapp.WSGIApplication([(r'/get.py', GetHandler)], debug=True) 

,並更改

class get(webapp.RequestHandler): 
    def post(self): 

class GetHandler(webapp.RequestHandler): 
    def post(self):