Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在PIL(低)中读取图像会破坏烧瓶图像端点?_Python_Image_Flask_Python Imaging Library_Pillow - Fatal编程技术网

Python 为什么在PIL(低)中读取图像会破坏烧瓶图像端点?

Python 为什么在PIL(低)中读取图像会破坏烧瓶图像端点?,python,image,flask,python-imaging-library,pillow,Python,Image,Flask,Python Imaging Library,Pillow,我正在使用Python构建一个网站。因为我将上传的图像存储在MongoDB中,所以我构建了一个简单的端点,通过id为图像提供服务: @app.route('/doc/<docId>') def getDoc(docId): userDoc = UserDocument.objects(id=docId).first() if not userDoc: return abort(404) return Response(userDoc.file

我正在使用Python构建一个网站。因为我将上传的图像存储在MongoDB中,所以我构建了一个简单的端点,通过id为图像提供服务:

@app.route('/doc/<docId>')
def getDoc(docId):
    userDoc = UserDocument.objects(id=docId).first()
    if not userDoc:
        return abort(404)

    return Response(userDoc.file_.read(), mimetype=userDoc.file_.content_type)
当我现在评论出问题所在的行并打开首页(其中加载了一些图像)时,所有图像都加载良好,我看到了这一点(正如预期的那样):

但是当我在上面粘贴代码时运行代码时(问题行没有注释),图像不会加载,我在终端中看到:

THUMBNAIL
THUMBNAIL
THUMBNAIL
NORMAL
NORMAL
NORMAL
212.xx.xx.xx - - [2015-03-19 16:58:11] "GET /doc/54e74956724b5907786e9918?width=100 HTTP/1.1" 200 138965 0.657734
212.xx.xx.xx - - [2015-03-19 16:58:11] "GET /doc/54e7495c724b5907786e991b?width=100 HTTP/1.1" 200 188871 0.753112
212.xx.xx.xx - - [2015-03-19 16:58:11] "GET /doc/5500c5d1724b595cf71b4d49?width=100 HTTP/1.1" 200 257495 1.024860
除了这些,我在终端中没有看到任何错误。当我尝试在浏览器中加载一个直接url时,它会显示
图像无法显示,因为它包含错误。
。我现在想知道两件事:

  • 尽管所有请求似乎都成功(200),但我在浏览器中看不到图像。为什么?
  • 即使需要一段时间才能将图像加载到枕头中(我认为不会),我仍然希望图像最终能够加载

  • 有人知道为什么当我把这些图片放进枕头里时,它们不被提供吗?欢迎所有提示

    枕头没什么问题。您的问题是您提供的是一个空响应。如果您正在提供缩略图,请枕头读取文件:

    然后尝试从同一个文件对象提供服务:


    我认为问题在于Image.open抛出了一个异常,该异常在某个地方被捕获,并且没有报告给控制台。您是否尝试过使用带有open('some/local/file.jpg')的
    作为f:Image.open(f)
    ?看看这是否会打印错误。@JDavidSmith-Hmm,你说得对。如果我做了
    im=Image.open('/tmp/some_Image_file.jpg')
    图像很好地服务。但我也在控制台中尝试了
    im=Image.open(userDoc.file)
    ,效果很好(包括调整大小并保存到其他位置)。另外:它会在最后打印出
    NORMAL
    ,所以看起来很好。但这确实表明MongoDB和枕头的结合存在问题。我正在研究更多内容。请尝试将语句包装在
    Try:。。。例外情况除外,如e:打印(e)升高e
    。这将打印任何错误并重新验证它(在修复格式后,因为无法在注释中换行)@JDavidSmith-尝试过;完全没有错误输出。我越来越认为它与MongoDB GridFS文件存储有关。或者你会有其他想法吗?我想我刚刚注意到了问题:
    Image.open
    将读取文件的全部内容,然后
    返回响应(userDoc.file\uuu.read(),…)
    。由于文件已被读取,该函数返回
    b”“
    ,这是一个无效图像。
    THUMBNAIL
    NORMAL
    THUMBNAIL
    NORMAL
    THUMBNAIL
    NORMAL
    212.xx.xx.xx - - [2015-03-19 16:57:02] "GET /doc/54e74956724b5907786e9918?width=100 HTTP/1.1" 200 139588 0.744827
    212.xx.xx.xx - - [2015-03-19 16:57:03] "GET /doc/54e7495c724b5907786e991b?width=100 HTTP/1.1" 200 189494 1.179268
    212.xx.xx.xx - - [2015-03-19 16:57:03] "GET /doc/5500c5d1724b595cf71b4d49?width=100 HTTP/1.1" 200 264593 1.416928
    
    THUMBNAIL
    THUMBNAIL
    THUMBNAIL
    NORMAL
    NORMAL
    NORMAL
    212.xx.xx.xx - - [2015-03-19 16:58:11] "GET /doc/54e74956724b5907786e9918?width=100 HTTP/1.1" 200 138965 0.657734
    212.xx.xx.xx - - [2015-03-19 16:58:11] "GET /doc/54e7495c724b5907786e991b?width=100 HTTP/1.1" 200 188871 0.753112
    212.xx.xx.xx - - [2015-03-19 16:58:11] "GET /doc/5500c5d1724b595cf71b4d49?width=100 HTTP/1.1" 200 257495 1.024860
    
    if desiredWidthStr or desiredHeightStr:    
        print 'THUMBNAIL'
        im = Image.open(userDoc.file_)  # reads from the file object
    
    if desiredWidthStr or desiredHeightStr:    
        print 'THUMBNAIL'
        # Load the image in Pillow
        im = Image.open(userDoc.file_)  # <=== THE PROBLEM!!!
        # TODO: resize and save the image in /tmp
    
    return Response(userDoc.file_.read(), mimetype=userDoc.file_.content_type)
    
    if desiredWidthStr or desiredHeightStr:    
        print 'THUMBNAIL'
        # Load the image in Pillow
        im = Image.open(userDoc.file_)
    
    print 'NORMAL'
    userDoc.file_.seek(0)  # ensure we are reading from the start
    return Response(userDoc.file_.read(), mimetype=userDoc.file_.content_type)