开向幼儿园的车 2022-04-04 11:44 采纳率: 81.8%
浏览 4016
已结题

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

在使用python flask 时,网页显示不出图片,只运行html文件可以,但运行py文件就不行,于是上网查资料发现不能直接贴上去
py 文件:
from flask import Flask,render_template

app = Flask(name)

def return_img_stream(image_loca_path): #向前端显示图片时使用,把图片转换成数据流
import base64
image_stream = ''
with open(image_loca_path,"r",encoding="UTF-8") as f: # 图片格式有问题
image_stream = f.read()
image_stream = base64.b64encode(image_stream)
return image_stream

@app.route("/")
def index():
image_path = "templates/images/bc.png"
image_stream = return_img_stream(image_path)
return render_template("index.html",img_stream=image_stream)

if name == "main":
app.run(host="0.0.0.0",debug=True)

html文件:

<title> my first python web </title> no way append

报错提示:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

img

  • 写回答

2条回答 默认 最新

  • ash062 2022-04-04 11:56
    关注

    不是应该用二进制模式传吗

    with open(image_loca_path,"rb") as f:
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月13日
  • 已采纳回答 4月5日
  • 创建了问题 4月4日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效