python - IOError:无法识别 prod 上的应用引擎的图像文件,但不能识别 localhost
问题描述
当我部署应用程序引擎时,我收到IOError: cannot identify image
了下面给出的代码,但它非常适用于 localhost。
我创建了第一个图像,但第二个图像在生产中失败,但在本地主机上都成功
在我的 app.yaml 中:
libraries:
- name: PIL
version: "1.1.7"
我也试过
libraries:
- name: PIL
version: latest
相关代码:
class UploadFile(webapp2.RequestHandler):
def post(self):
data = self.request.body
values = self.request.POST.itervalues()
files = [v for v in values if isinstance(v, cgi.FieldStorage)]
img_data = data
dataBytesIO = io.BytesIO(img_data) #log label "toto"
my_img = Image.open(files[0].file)
my_img.thumbnail((500, 500), Image.ANTIALIAS)
thumb_io = BytesIO()
my_img.save(thumb_io, my_img.format)
thumbnail_filename_input = create_file(self, thumb_io.getvalue(), "room_picture_thumbnail_" + "somename", files[0].type)
size = 1000, 1000 #log label "titi"
my_img_big = Image.open(files[0].file) #!!! Fails on production but works on localhost
width, height = my_img_big.size
if width > 1000 or height > 1000:
my_img_big.thumbnail(size,Image.ANTIALIAS)
thumb_io2 = BytesIO()
my_img_big.save(thumb_io2, my_img_big.format)
filename_input = create_file(self, thumb_io2.getvalue(), "room_picture_" +"somename", files[0].type)
给出以下错误:
my_img_big = Image.open(files[0].file)
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/b3d733487011db10/python27/python27_lib/versions/third_party/PIL-1.1.7/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
并记录应用引擎堆栈驱动程序调试的快照:
files
[0]
fp
<type '_io.BytesIO'>
disposition_options
file
<type 'cStringIO.StringO'>
innerboundary
''
done
1
disposition
'form-data'
qs_on_post
None
strict_parsing
0
outerboundary
'----WebKitFormBoundarykEX5mAPk1xmDW6B3' # only this line differs between two -> '----WebKitFormBoundaryZxbkkzZRFdunz9Fr' at log labels "toto" and "titi"
name
u'aa.png'
_FieldStorage__file
None
list
None
filename
u'/frpy/uploadFile'
keep_blank_values
True
headers
length
-1
type_options
type
'image/png'
我什至尝试在本地应用程序引擎产品上使用以下代码
remote_api_stub.ConfigureRemoteApiForOAuth(
'{}.appspot.com'.format(project_id),
'/_ah/remote_api')
和代码:
import urllib2 as urllib
import io
fd = urllib.urlopen("https://i.stack.imgur.com/mVt7n.jpg?s=328&g=1")
image_file = io.BytesIO(fd.read())
im = Image.open(image_file)
im.thumbnail((500, 500), Image.ANTIALIAS)
thumb_io = BytesIO()
im.save(thumb_io, im.format)
size = 1000, 1000
my_img_big = Image.open(image_file)
width, height = my_img_big.size
if width > 1000 or height > 1000:
my_img_big.thumbnail(size,Image.ANTIALIAS)
thumb_io2 = BytesIO()
my_img_big.save(thumb_io2, my_img_big.format)
运行两次image_file
效果很好。
什么问题在 localhost 上有效,但在 prod 上失败?
提示:my_img.close() 适用于 localhost 但不适用于 prod,还请注意,当我files[0].file.close()
在 localhost 上进行计算时,它给了我ValueError: I/O operation on closed file
,因此问题与关闭无关
解决方案
files[0].file.seek(0)
是答案,我应该倒带它:)
推荐阅读
- r - R:从自身中减去第一行,从列中的下一行减去后续行,并对多列执行此操作
- settimeout - ServiceNow:recordWatch 和 $rootscope 计时问题
- c++ - 如何修复重载操作数+?
- python - 从csv文件python 3打印时如何删除[]和''
- android - 交叉编译时NDK工具链中的/system/bin/linker在哪里
- angularjs - 在多列Angular智能表上实现数字范围搜索
- r - 如何在不影响 R 中的数字的情况下反转字符串?
- vector - 如何创建并附加到 Vec
? - c# - PopModalAsync 将数据传递给父级
- python - 分水岭算法检测到的物体面积