如何在不保存的情况下直接将Flask图像显示在HTML中?

huangapple go评论106阅读模式
英文:

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 = &quot;image/jpeg&quot;
    uri = &quot;data:%s;base64,%s&quot; % (mime, encoded)

    return render_template(&#39;/result.html&#39;,uri=uri)

html code:

&lt;img src=&quot;{{ uri }}&quot;&gt;

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(&#39;uint8&#39;))
img.save(file_object, &#39;PNG&#39;)
base64img = &quot;data:image/png;base64,&quot;+b64encode(file_object.getvalue()).decode(&#39;ascii&#39;)

HTML:

&lt;img src=&quot;{{base64img}}&quot;      &gt;

答案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 == &#39;POST&#39;:
        f = request.files[&#39;file&#39;].read()
        #    print(f)
        npimg = np.fromstring(f,np.uint8)
        img = cv2.imdecode(npimg,cv2.IMREAD_COLOR)
        img = Image.fromarray(img.astype(&quot;uint8&quot;))
        rawBytes = io.BytesIO()
        img.save(rawBytes, &quot;JPEG&quot;)
        rawBytes.seek(0)
        img_base64 = base64.b64encode(rawBytes.getvalue()).decode(&#39;ascii&#39;)
        mime = &quot;image/jpeg&quot;
        uri = &quot;data:%s;base64,%s&quot;%(mime, img_base64)
        return render_template(&quot;./template/output.html&quot;,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, &#39;JPEG&#39;)
    mem_bytes.seek(0)
    img_base64 = base64.b64encode(mem_bytes.getvalue()).decode(&#39;ascii&#39;)
    mime = &quot;image/jpeg&quot;
    uri = &quot;data:%s;base64,%s&quot;%(mime, img_base64)
    return render_template(&quot;main.html&quot;, image=uri)

NP_IMAGE = (np.random.random((300,400, 3)) * 255).astype(&quot;uint8&quot;)

@app.route(&quot;/&quot;, methods=[&#39;GET&#39;])
def main():
    return render_frame(NP_IMAGE)


the html code:

&lt;img src=&quot;{{ image }}&quot;&gt;

huangapple
  • 本文由 发表于 2020年1月3日 23:55:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定