我有一个Flask应用程序,该应用程序运行Selenium来通过XPATH提取图像。 将.JPG图像保存到MySQL DB(二进制),然后应将其呈现为HTML。问题在于它没有被渲染。
有什么想法吗?
profile_picture = Column(BLOB)
profile_picture = WebDriverWait(browser, 2).until(EC.presence_of_element_located((By.XPATH,
"//*[@id='react-root']/section/main/div/button/img")))
src = profile_picture.get_attribute('src')
settings_db.profile_picture = profile_picture
profile_picture = TABLE.query.filter_by(username=current_user.username).first()
profile_picture = profile_picture.profile_picture
return render_template('index.html', profile_picture=profile_picture)
<img src="{{ profile_picture }}" alt="User Image">
图像从不显示,并且大括号之间的HTML输出为“未知”。
答案 0 :(得分:1)
我认为二进制对象不应该在层之间传输。您是否尝试过将图像转换为Base64 src格式?
<div>
<p>Taken from wikpedia</p>
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>
您可以使用此方法:
encoded_image = base64.b64encode(profile_picture).decode()
encoded_image_str = "data:image/png;base64,{encoded_image}".format(encoded_image=encoded_image)