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

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日

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试