首页 > 解决方案 > 在将图像上传到 s3 存储桶之前压缩图像

问题描述

我想在将用户的个人资料图片上传到我的 s3 存储桶之前对其进行压缩。我有一个烧瓶应用程序。

这是我的代码:

@app.route("/profile", methods=["GET", "POST"])
@login_required
def profile():
        if request.method == "POST":
            user_id = session["user_id"]
            if request.files['photo']:
                filename = photos.save(request.files['photo'])
                ## here should go the code to compress the image
                s3 = boto3.resource('s3')
                s3.Bucket('mybucketname').upload_file("static/img/" + filename, filename)
                row = User.query.filter_by(user_id = user_id).first()
                row.img_url = filename
                updated_filename = row.img_url
                db.session.commit()

我是 python 新手。我尝试使用 PIL 实现一个,但未能调整教程中的代码。如果你们能帮助我,我将不胜感激。

标签: pythonflask

解决方案


推荐阅读