在Google Appengine SDK中使用PIL遇到问题
我有一台运行Windows Server 2008 R2(64位)的电脑,我想在上面开发一个谷歌的AppEngine应用。
于是我从python.org上安装了Python 2.5.4(因为谷歌的SDK说我需要2.5版本,而2.5.6没有安装包)。接着,我从http://www.pythonware.com/products/pil/上安装了PIL,使用的是适用于Python 2.5的1.1.7版本。我使用的是这两个软件的32位版本。
然后我安装了AppEngine SDK。
Hello-World程序运行得很好,但我想处理一张图片时却出现了问题,得到了一个堆栈跟踪和HTTP 500的响应:
2010-02-18 11:50:27 Running command: "['C:\\Python25\\pythonw.exe', 'C:\\Program Files
(x86)\\Google\\google_appengine\\dev_appserver.py', '--admin_console_server=', '--port=8080', 'd:\\imgsvc']"
WARNING 2010-02-18 10:50:29,260 datastore_file_stub.py:623] Could not read datastore data from c:\users\admini~1\appdata\local\temp\dev_appserver.datastore
INFO 2010-02-18 10:50:29,627 dev_appserver_main.py:399] Running application imgsvc on port 8080: http://localhost:8080
ERROR 2010-02-18 10:50:40,058 dev_appserver.py:3217] Exception encountered handling request
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3180, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3123, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 515, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2382, in Dispatch
self._module_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2292, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2188, in ExecuteOrImportScript
exec module_code in script_module.__dict__
File "d:\imgsvc\imgsvc.py", line 7, in <module>
outputimage = images.resize(inputimage.content, 32, 32)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\images\__init__.py", line 625, in resize
return image.execute_transforms(output_encoding=output_encoding)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\images\__init__.py", line 513, in execute_transforms
response)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 78, in MakeSyncCall
return apiproxy.MakeSyncCall(service, call, request, response)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 278, in MakeSyncCall
rpc.CheckSuccess()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_rpc.py", line 149, in _WaitImpl
self.request, self.response)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub.py", line 80, in MakeSyncCall
method(request, response)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\images\images_stub.py", line 171, in _Dynamic_Transform
response_value = self._EncodeImage(new_image, request.output())
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\images\images_stub.py", line 193, in _EncodeImage
image.save(image_string, image_encoding)
File "C:\Python25\lib\site-packages\PIL\Image.py", line 1439, in save
save_handler(self, fp, filename)
File "C:\Python25\lib\site-packages\PIL\PngImagePlugin.py", line 564, in _save
import ICCProfile
SystemError: Parent module 'PIL' not loaded
INFO 2010-02-18 10:50:40,081 dev_appserver.py:3246] "GET / HTTP/1.1" 500 -
我尝试运行的Python脚本是:
from google.appengine.api import urlfetch
from google.appengine.api import images
url = "http://www.brokenwire.net/bw/images/113.png"
inputimage = urlfetch.fetch(url)
if inputimage.status_code == 200:
outputimage = images.resize(inputimage.content, 32, 32)
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(outputimage)
有没有人知道这里出了什么问题吗?
我还尝试了一个独立的Python脚本,它运行得很好:
import Image
im = Image.open('filename.png')
im2 = im.resize((100,100), Image.ANTIALIAS)
im2.show()
看起来我使用的图片不同会有影响:
url = "http://www.r-stone.net/blogs/ishikawa/uploaded_images/google_appengine-779483.png"
这个图片给出了问题的堆栈跟踪,但
url = "http://www.brokenwire.net/bw/images/113.png"
这个图片就没有问题。
3 个回答
我想PIL在App Engine上是不能用的。目前有629个人已经标记了这个问题,希望能支持Python图像库(PIL),这个问题在App Engine上是第五个最受关注的。我也鼓励你去标记这个问题。
我在我的Windows电脑上遇到了完全一样的问题(在MacOS上是可以正常工作的),所以我卸载了PIL 1.1.7版本,然后安装了之前的1.1.6版本,现在一切都正常了;)。
这里有个解决这个问题的来源: http://www.chris-wong.net/parent-module-pil-not-loaded/
这个问题发生在Python找不到ICCProfile模块的时候。显然,当通过GAE使用时,不是出现导入错误(ImportError),而是导入器抛出了一个系统错误(SystemError),导致函数执行失败。我所做的就是把...\\Python25\\Lib\\site-packages\\PIL\\PngImagePlugin.py文件中的第567行从
except ImportError:
改成:
except Exception: