英文:
How to display Flask image to HTML directly without saving?
问题
flask 代码:
from base64 import b64encode
def abc():
encoded = b64encode(img_data)
mime = "image/jpeg"
uri = "data:%s;base64,%s" % (mime, encoded)
return render_template('/result.html', uri=uri)
HTML 代码:
<img src="{{ uri }}">
我尝试了这个,但图片没有显示出来。有任何想法吗?我有一个在numpy数组中的图像。
英文:
flask code:
from base64 import b64encode
def abc()
encoded = b64encode(img_data)
mime = "image/jpeg"
uri = "data:%s;base64,%s" % (mime, encoded)
return render_template('/result.html',uri=uri)
html code:
<img src="{{ uri }}">
I tried this, but the image is not getting displayed. Any idea how? I have image in a numpy array.
答案1
得分: 4
file_object = io.BytesIO()
img= Image.fromarray(originalimg.astype('uint8'))
img.save(file_object, 'PNG')
base64img = "data:image/png;base64,"+b64encode(file_object.getvalue()).decode('ascii')
HTML:
<img src="{{base64img}}" >
英文:
Solution:
file_object = io.BytesIO()
img= Image.fromarray(originalimg.astype('uint8'))
img.save(file_object, 'PNG')
base64img = "data:image/png;base64,"+b64encode(file_object.getvalue()).decode('ascii')
HTML:
<img src="{{base64img}}" >
答案2
得分: 1
以下是您要翻译的内容:
这只是上述解决方案的补充部分,我花了大约12个小时来寻找这个,感谢@john_doe,在这里是我的解决方案与上述解决方案相结合,希望也能帮助其他人:
def upload_file():
if request.method == 'POST':
f = request.files['file'].read()
# print(f)
npimg = np.fromstring(f, np.uint8)
img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
img = Image.fromarray(img.astype("uint8"))
rawBytes = io.BytesIO()
img.save(rawBytes, "JPEG")
rawBytes.seek(0)
img_base64 = base64.b64encode(rawBytes.getvalue()).decode('ascii')
mime = "image/jpeg"
uri = "data:%s;base64,%s" % (mime, img_base64)
return render_template("./template/output.html", image=uri)
希望对您有帮助。
英文:
this is just the complementary solution for the above solution, I was searching for this for like 12 hours, thanks @john_doe, here is my solution combined with the above solution I hope it also helps someone:
def upload_file():
if request.method == 'POST':
f = request.files['file'].read()
# print(f)
npimg = np.fromstring(f,np.uint8)
img = cv2.imdecode(npimg,cv2.IMREAD_COLOR)
img = Image.fromarray(img.astype("uint8"))
rawBytes = io.BytesIO()
img.save(rawBytes, "JPEG")
rawBytes.seek(0)
img_base64 = base64.b64encode(rawBytes.getvalue()).decode('ascii')
mime = "image/jpeg"
uri = "data:%s;base64,%s"%(mime, img_base64)
return render_template("./template/output.html",image=uri)
答案3
得分: -1
以下是代码的中文翻译部分:
这是对上面的sameer maurya的解决方案的补充解决方案。
Python 代码:
import io
import numpy as np
from PIL import Image
import base64
def render_frame(arr: np.ndarray):
mem_bytes = io.BytesIO()
img = Image.fromarray(arr)
img.save(mem_bytes, 'JPEG')
mem_bytes.seek(0)
img_base64 = base64.b64encode(mem_bytes.getvalue()).decode('ascii')
mime = "image/jpeg"
uri = "data:%s;base64,%s"%(mime, img_base64)
return render_template("main.html", image=uri)
NP_IMAGE = (np.random.random((300,400, 3)) * 255).astype("uint8")
@app.route("/", methods=['GET'])
def main():
return render_frame(NP_IMAGE)
HTML 代码:
<img src="{{ image }}">
请注意,代码中的特殊字符和格式保持不变。
英文:
This is a complementary solution to the one above of sameer maurya,
The python code:
import io
import numpy as np
from PIL import Image
import base64
def render_frame(arr: np.ndarray):
mem_bytes = io.BytesIO()
img = Image.fromarray(arr)
img.save(mem_bytes, 'JPEG')
mem_bytes.seek(0)
img_base64 = base64.b64encode(mem_bytes.getvalue()).decode('ascii')
mime = "image/jpeg"
uri = "data:%s;base64,%s"%(mime, img_base64)
return render_template("main.html", image=uri)
NP_IMAGE = (np.random.random((300,400, 3)) * 255).astype("uint8")
@app.route("/", methods=['GET'])
def main():
return render_frame(NP_IMAGE)
the html code:
<img src="{{ image }}">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论