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

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

How to display Flask image to HTML directly without saving?

问题

flask 代码:

  1. from base64 import b64encode
  2. def abc():
  3. encoded = b64encode(img_data)
  4. mime = "image/jpeg"
  5. uri = "data:%s;base64,%s" % (mime, encoded)
  6. return render_template('/result.html', uri=uri)

HTML 代码:

  1. <img src="{{ uri }}">

我尝试了这个,但图片没有显示出来。有任何想法吗?我有一个在numpy数组中的图像。

英文:

flask code:

  1. from base64 import b64encode
  2. def abc()
  3. encoded = b64encode(img_data)
  4. mime = &quot;image/jpeg&quot;
  5. uri = &quot;data:%s;base64,%s&quot; % (mime, encoded)
  6. return render_template(&#39;/result.html&#39;,uri=uri)

html code:

  1. &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

  1. file_object = io.BytesIO()
  2. img= Image.fromarray(originalimg.astype('uint8'))
  3. img.save(file_object, 'PNG')
  4. base64img = "data:image/png;base64,"+b64encode(file_object.getvalue()).decode('ascii')
  5. HTML:
  6. <img src="{{base64img}}" >
英文:

Solution:

  1. file_object = io.BytesIO()
  2. img= Image.fromarray(originalimg.astype(&#39;uint8&#39;))
  3. img.save(file_object, &#39;PNG&#39;)
  4. base64img = &quot;data:image/png;base64,&quot;+b64encode(file_object.getvalue()).decode(&#39;ascii&#39;)

HTML:

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

答案2

得分: 1

以下是您要翻译的内容:

这只是上述解决方案的补充部分,我花了大约12个小时来寻找这个,感谢@john_doe,在这里是我的解决方案与上述解决方案相结合,希望也能帮助其他人:

  1. def upload_file():
  2. if request.method == 'POST':
  3. f = request.files['file'].read()
  4. # print(f)
  5. npimg = np.fromstring(f, np.uint8)
  6. img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
  7. img = Image.fromarray(img.astype("uint8"))
  8. rawBytes = io.BytesIO()
  9. img.save(rawBytes, "JPEG")
  10. rawBytes.seek(0)
  11. img_base64 = base64.b64encode(rawBytes.getvalue()).decode('ascii')
  12. mime = "image/jpeg"
  13. uri = "data:%s;base64,%s" % (mime, img_base64)
  14. 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:

  1. def upload_file():
  2. if request.method == &#39;POST&#39;:
  3. f = request.files[&#39;file&#39;].read()
  4. # print(f)
  5. npimg = np.fromstring(f,np.uint8)
  6. img = cv2.imdecode(npimg,cv2.IMREAD_COLOR)
  7. img = Image.fromarray(img.astype(&quot;uint8&quot;))
  8. rawBytes = io.BytesIO()
  9. img.save(rawBytes, &quot;JPEG&quot;)
  10. rawBytes.seek(0)
  11. img_base64 = base64.b64encode(rawBytes.getvalue()).decode(&#39;ascii&#39;)
  12. mime = &quot;image/jpeg&quot;
  13. uri = &quot;data:%s;base64,%s&quot;%(mime, img_base64)
  14. return render_template(&quot;./template/output.html&quot;,image=uri)

答案3

得分: -1

以下是代码的中文翻译部分:

这是对上面的sameer maurya的解决方案的补充解决方案。

Python 代码:

  1. import io
  2. import numpy as np
  3. from PIL import Image
  4. import base64
  5. def render_frame(arr: np.ndarray):
  6. mem_bytes = io.BytesIO()
  7. img = Image.fromarray(arr)
  8. img.save(mem_bytes, 'JPEG')
  9. mem_bytes.seek(0)
  10. img_base64 = base64.b64encode(mem_bytes.getvalue()).decode('ascii')
  11. mime = "image/jpeg"
  12. uri = "data:%s;base64,%s"%(mime, img_base64)
  13. return render_template("main.html", image=uri)
  14. NP_IMAGE = (np.random.random((300,400, 3)) * 255).astype("uint8")
  15. @app.route("/", methods=['GET'])
  16. def main():
  17. return render_frame(NP_IMAGE)

HTML 代码:

  1. <img src="{{ image }}">

请注意,代码中的特殊字符和格式保持不变。

英文:

This is a complementary solution to the one above of sameer maurya,

The python code:

  1. import io
  2. import numpy as np
  3. from PIL import Image
  4. import base64
  5. def render_frame(arr: np.ndarray):
  6. mem_bytes = io.BytesIO()
  7. img = Image.fromarray(arr)
  8. img.save(mem_bytes, &#39;JPEG&#39;)
  9. mem_bytes.seek(0)
  10. img_base64 = base64.b64encode(mem_bytes.getvalue()).decode(&#39;ascii&#39;)
  11. mime = &quot;image/jpeg&quot;
  12. uri = &quot;data:%s;base64,%s&quot;%(mime, img_base64)
  13. return render_template(&quot;main.html&quot;, image=uri)
  14. NP_IMAGE = (np.random.random((300,400, 3)) * 255).astype(&quot;uint8&quot;)
  15. @app.route(&quot;/&quot;, methods=[&#39;GET&#39;])
  16. def main():
  17. return render_frame(NP_IMAGE)

the html code:

  1. &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:

确定