Spaces:
Paused
Paused
File size: 2,034 Bytes
1cac303 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import dominate
from dominate.tags import *
import os
import base64
import numpy as np
from io import BytesIO
from PIL import Image
import argparse
import random
import cv2
class SimpleHtml():
def __init__(self, html_file='./index.html', refresh=0):
self.html_file = html_file
self.doc = dominate.document(title='simple_html')
if refresh > 0:
with self.doc.head:
meta(http_equiv="reflesh", content=str(refresh))
def newline(self):
self.table = table(border=1, style="table-layout: fixed;")
self.doc.add(self.table)
self.tr = tr()
self.table.add(self.tr)
self._save()
def add_image(self, im, txt, height=400, isbgr=True):
_td = td(style="word-wrap: break-word;", halign="center", valign="top")
with _td:
with p():
imgstr = self._im2str(im, isbgr)
img(style="height:%dpx" % height,
src="data:image/jpg;base64,%s" % imgstr)
br()
p(txt)
self.tr.add(_td)
self._save()
def _im2str(self, im, isbgr):
if len(im.shape)==3:
if isbgr:
pil_image = Image.fromarray(im[:,:,::-1])
else:
pil_image = Image.fromarray(im)
else:
pil_image = Image.fromarray(im)
buff = BytesIO()
pil_image.save(buff, format="JPEG")
imgstr = base64.b64encode(buff.getvalue()).decode("utf-8")
return imgstr
def _save(self):
with open(self.html_file, 'wt') as f:
f.write(self.doc.render())
if __name__ == '__main__':
html = SimpleHtml()
html.newline()
image = np.zeros((50, 60, 3), dtype=np.uint8) + 128
html.add_image(image, 'test')
image = np.zeros((50, 60), dtype=np.uint8) + 128
html.add_image(image, 'test')
html.newline()
image = np.zeros((50, 60), dtype=np.uint8) + 128
html.add_image(image, 'test')
|