我正在尝试调整上传文件的大小。到目前为止,我确信图像已正确加载,并创建了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)