如何在将图像保存到文件夹之前调整图像的大小,使其在使用flask的bootstrap 4卡中具有固定的高度?

2024-04-20 16:34:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有4张引导卡,我正在使用卡img top使图像具有响应性。唯一的问题是图像大小不一,我使用AJAX将这些图像输入后端。 我想缩小图像的大小(高度和宽度),使其既具有响应性又具有固定长度

下面是我的上传视图,我一提交表单就给它打电话

@app.route('/', methods=['POST'])
def upload():
if 'photos[]' in request.files:
  photos = request.files.getlist('photos[]')
  photo = photos[0]

  label = request.form["label"]
  email = request.form["email"]

  img_formate = "." + str(photo.filename).split(".")[-1] 
  image = label + img_formate

  #i just want to resize image to width = 360 px and height to 300px
  photo.save(os.path.join(app.config['UPLOAD_FOLDER'], image))

  return " "

Tags: to图像imageformappimgemailrequest
1条回答
网友
1楼 · 发布于 2024-04-20 16:34:36

谢谢,@srikanth Chekuri帮助我

@app.route('/', methods=['POST'])
def upload():
if 'photos[]' in request.files:
  photos = request.files.getlist('photos[]')
  photo = photos[0]

  label = request.form["label"]
  email = request.form["email"]

  img_formate = "." + str(photo.filename).split(".")[-1] 
  image = label + img_formate

  #this worked for me.
  photo = Image.open(photo)
  photo = photo.resize((400,300))

  photo.save(os.path.join(app.config['UPLOAD_FOLDER'], image))

return " "

相关问题 更多 >