文件上传采用的是flask-uploads插件,添加了一个改变文件名的函数change_filename,现在希望可以增加一个缩放尺寸的函数,自定义宽高,只保存缩放后的图片,目前代码如下:@admin.route('/post/new', methods=['GET', 'POST'])@login_required
def post_new():
form = PostForm() if form.validate_on_submit():
title = form.title.data
photo_filename = change_filename(form.photo.data.filename)
photo = photos.save(form.photo.data, name=photo_filename)
post = Post(title=title, photo=photo)
db.session.add(post)
db.session.commit() return redirect(url_for('admin.post_manage')) return render_template('admin/post_new.html', form=form)请教大虾们应该怎么写!感谢!
1 回答
白衣染霜花
TA贡献1796条经验 获得超10个赞
from PIL import Image old_file = "1.jpg"new_file = "1_t.jpg"sImg = Image.open(old_file) w, h = sImg.sizeprint(w,h) dImg = sImg.resize((int(w / 2), int(h / 2)), Image.ANTIALIAS) dImg.save(new_file)print("转换后尺寸") sImg_t = Image.open(new_file) w, h = sImg_t.sizeprint(w,h)
添加回答
举报
0/150
提交
取消