我正在通过烧瓶后端上的PILLOW调整图像的大小,但是当到达代码行.resize时,它失败了吗? - python

我正在尝试调整上传文件的大小。到目前为止,我相信图像已正确加载,并且创建了PILLOW图像类。它通过我的调整大小脚本运行,但是随后总是在.resize代码上停止...

我在桌面上(而不是在服务器上)运行代码,并且图像调整大小有效,但是当我将调整大小脚本与通过POST上传的图像结合使用时,它无法正常工作并显示500错误。这是怎么回事?

我在imageresizer代码后立即使用了print imageThumbnail.size并得到了AttributeError: 'NoneType' object has no attribute 'size'

def imageResizer(im, pixellimit):

  width, height = im.size

  if width > height:
    #Land scape mode. Scale to width.

    aspectRatio = float(height)/float(width)
    Scaledwidth = pixellimit
    Scaledheight = int(round(Scaledwidth * aspectRatio))
    newSize = (Scaledwidth, Scaledheight)
  elif height > width:
    #Portrait mode, Scale to height.
    aspectRatio = float(width)/float(height)
    Scaledheight = pixellimit
    Scaledwidth = int(round(Scaledheight * aspectRatio))
    newSize = (Scaledwidth, Scaledheight)

  #FAILS RIGHT HERE... I double checked by writing print flags all over, and it so happens nothing past this line gets written
  imageThumbnail = im.resize(newSize)

  return imageThumbnail

这是FLask框架的一部分。

file = request.files['file']
    location = str(args['lat']) + str(args['lon'])
    location = location.replace('.','_')
    GUID = datetime.strftime(datetime.now(), '%Y%m%d%H%M%S') + location
    datetimeEntry = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
    fullFileName = GUID + '.' + file.filename.rsplit('.', 1)[1]
    if file and allowed_file(file.filename):
      filename = secure_filename(file.filename)

      image = Image.open(file)
      imageThumbnail = imageResizer(image, 800)
      #NOTHING PAST THIS POINT GETS EXECUTED
      imageThumbnailName = GUID + "thumb" + '.' + file.filename.rsplit('.', 1)[1]
      imageThumbnailName.save(os.path.join(app.config['UPLOAD_FOLDER'], imageThumbnailName))
      file.save(os.path.join(app.config['UPLOAD_FOLDER_LARGE_IMAGES'], fullFileName))

参考方案

问题是您正在尝试打开:


file = request.files['file']
image = Image.open(file)

file不是实际文件,而是带有上载信息的某些元数据对象。相反,您应该做的是:


image = Image.open(file.stream)

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python:传递记录器是个好主意吗? - python

我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…

Python:如何根据另一列元素明智地查找一列中的空单元格计数? - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…