【发布时间】:2018-02-20 22:38:24
【问题描述】:
它显示错误 LookupError: 'base64' is not a text encoding;使用 codecs.decode() 处理任意编解码器。
执行停止时的最后几行是 - thumbnail = resize(photo_data, 200, 200) 文件“/home/anurag/photoshare/app.py”,第 406 行,调整大小 image_string = StringIO(img.decode('base64')) LookupError: 'base64' 不是文本编码;使用 codecs.decode() 处理任意编解码器
开始照片上传代码
使用 base64 编码上传的照片,因此可以直接嵌入 HTML
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):``
return '.' in filename and filename.rsplit('.', 1)[1] in
ALLOWED_EXTENSIONS
@app.route('/upload', methods=['GET', 'POST'])
@flask_login.login_required
def upload_file():
if request.method == 'POST':
uid = getUserIdFromEmail(flask_login.current_user.id)
imgfile = request.files['photo']
caption = request.form.get('caption')
album_id = request.form.get('album')
tags = request.form.get('tags')
photo_data = base64.standard_b64encode(imgfile.read())
thumbnail = resize(photo_data, 200, 200)
cursor = conn.cursor()
cursor.execute("INSERT INTO Photos (imgdata, thumbnail, user_id,
caption, album_id) VALUES ('{0}', '{1}', '{2}', '{3}',
'{4}')".format(photo_data, thumbnail, uid, caption, album_id))
conn.commit()
picture_id = cursor.lastrowid
print (picture_id)
print (tags)
insert_tags(picture_id, tags)
return redirect( url_for('get_album', id=album_id))
#The method is GET so we return a HTML form to upload the a photo.
else:
return render_template('upload.html', loggedin=True,
albums=get_all_album_data())
结束照片上传代码
如果大于 (x,y) 则调整大小,否则返回原始值
def resize(img, x,y):
image_string = StringIO(img.decode('base64'))
with Image.open(image_string) as image:
if image.size[0] > 200 and image.size[1] > 200:
cover = resizeimage.resize_cover(image, [x,y])
buffer = StringIO()
cover.save(buffer, format="JPEG")
return base64.b64encode(buffer.getvalue())
else:
return img
然后还有另一个图像文件。 从 PIL 导入图像 从 io 导入 StringIO 从调整大小图像导入调整大小图像 导入操作系统,base64
def resize(img, x,y):
image_string = StringIO(img.decode('base64'))
``with Image.open(image_string) as image:
if image.size[0] > 200 and image.size[1] > 200:
cover = resizeimage.resize_cover(image, [x,y])
return base64.standard_b64encode(cover)
else:
return img``
【问题讨论】: