代码之家  ›  专栏  ›  技术社区  ›  asdf_enel_hak

IOError:无法识别prod上应用程序引擎的图像文件,但无法识别localhost

  •  0
  • asdf_enel_hak  · 技术社区  · 6 年前

    我收到 IOError: cannot identify image

    我创建了第一个映像,但在生产环境中第二个映像失败,但在本地主机上都成功了

    在my 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'
    

    我甚至试着从本地的appengine prod上下载代码

    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

    0 回复  |  直到 6 年前
        1
  •  0
  •   asdf_enel_hak    6 年前
    files[0].file.seek(0) 
    

    推荐文章