Hac*_*ack 4 html python base64 image
我想创建一个图像(不将其保存到磁盘),并在浏览器中显示它。据我所知,我必须创建一个 base64 图像,但我不知道如何制作。
你能帮助我吗?
这是我的代码:
from PIL import Image, ImageDraw
import base64
im = Image.new('RGBA', (200, 100), color = 'black')
data_uri = #missing how I can convert the image to base64 ?
html = '<html><head></head><body>'
html += '<img src="data:image/png;base64,{0}">'.format(data_uri)
html += '</body></html>'
print (html)
Run Code Online (Sandbox Code Playgroud)
Sta*_*nov 13
您需要将图像转换为正确的格式(在本例中为 PNG)作为缓冲区,然后对缓冲区进行编码。
from PIL import Image, ImageDraw
import base64
import io
im = Image.new('RGBA', (200, 100), color = 'black')
buffer = io.BytesIO()
im.save(buffer, format='PNG')
buffer.seek(0)
data_uri = base64.b64encode(buffer.read()).decode('ascii')
html = '<html><head></head><body>'
html += '<img src="data:image/png;base64,{0}">'.format(data_uri)
html += '</body></html>'
print (html)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6153 次 |
| 最近记录: |