Skip to main content
 首页 » 编程设计

python之在 App Engine 上用 Python 打开图像

2025年04月02日39leader

所以我有一段代码,当我在我的控制台中运行它时它工作得很好,但是当我尝试在 App Engine 上运行它时却没有。

from lib.PIL import Image 
import urllib, cStringIO 
@endpoints.method(ImageCreateRequest, DummyResponse, http_method='POST', name='image_create') 
def image_create(self, request): 
    file = cStringIO.StringIO(urllib.urlopen(URL).read()) 
    img = Image.open(file) 
    img.load() 
    return DummyResponse( 
        response=str(img.size) 
    ) 

我得到以下错误

在_新

new.mode = im.mode 
 
AttributeError: 'NoneType' object has no attribute 'mode' 

编辑:精确回溯

Encountered unexpected error from ProtoRPC method implementation: AttributeError ('NoneType' object has no attribute 'mode') 
 
Traceback (most recent call last): 
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app 
response = method(instance, request) 
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/endpoints-1.0/endpoints/api_config.py", line 1331, in invoke_remote 
return remote_method(service_instance, request) 
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/remote.py", line 414, in invoke_remote_method 
response = method(service_instance, request) 
File "/base/data/home/apps/myproj/1.392969200987483659/post_api.py", line 100, in image_create 
img = Image.open(file) 
File "/base/data/home/apps/myproj/1.392969200987483659/lib/PIL/Image.py", line 1969, in open 
if init(): 
File "/base/data/home/apps/myproj/1.392969200987483659/lib/PIL/Image.py", line 355, in init 
__import__(f, globals(), locals(), []) 
File "/base/data/home/apps/myproj/1.392969200987483659/lib/PIL/PalmImagePlugin.py", line 92, in <module> 
Palm8BitColormapImage = build_prototype_image() 
File "/base/data/home/apps/myproj/1.392969200987483659/lib/PIL/PalmImagePlugin.py", line 82, in build_prototype_image 
image = Image.new("L", (1,len(_Palm8BitColormapValues),)) 
File "/base/data/home/apps/myproj/1.392969200987483659/lib/PIL/Image.py", line 1763, in new 
return Image()._new(core.fill(mode, size, color)) 
File "/base/data/home/apps/myproj/1.392969200987483659/lib/PIL/Image.py", line 463, in _new 
new.mode = im.mode 

AttributeError: 'NoneType' 对象没有属性 'mode'

我不太确定为什么如此简单的东西会在控制台中运行,但在 App Engine 上却无法运行。有什么想法吗?

谢谢

请您参考如下方法:

如果这是一个生产问题,你应该试试这个 - 从您自己安装的 lib 文件夹中删除 PIL 模块。

转到 app.yaml 并将其粘贴到库部分下(如果没有一个粘贴整个)-

库: - 名称:PIL 版本:最新

调用基于应用引擎的模块 -

from PIL import Image