Skip to content
Permalink
Browse files

Initial implementation

  • Loading branch information...
dchaplinsky committed Oct 16, 2014
1 parent 26da306 commit 28682d534131e8f94ae82d73e32816ac7916afdb
Showing with 129 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +25 −0 demo.py
  3. BIN lenna.jpg
  4. 0 setup.py
  5. +101 −0 vlogging/__init__.py
@@ -52,3 +52,6 @@ docs/_build/

# PyBuilder
target/

.DS_Store
test.html
25 demo.py
@@ -0,0 +1,25 @@
from logging import FileHandler
from vlogging import VisualRecord

if __name__ == '__main__':
import cv2
from PIL import Image

cv_image = cv2.imread('lenna.jpg')
pil_image = Image.open('lenna.jpg')

import logging
logger = logging.getLogger("demo")
fh = FileHandler('test.html', mode="w")

logger.setLevel(logging.DEBUG)
logger.addHandler(fh)

logger.debug(VisualRecord(
"Hello from OpenCV", cv_image, "multi\n\nline", fmt="png"))

logger.info(VisualRecord(
"Hello from PIL", pil_image, "annotation", fmt="jpeg"))

logger.warning(
VisualRecord("Hello from both", [cv_image, pil_image], fmt="jpeg"))
BIN +8.01 KB lenna.jpg
Binary file not shown.
No changes.
@@ -0,0 +1,101 @@
# -*- coding: utf-8 -*-

import StringIO
from string import Template
import base64

renderers = []

try:
import cv2
import numpy

def render_opencv(img, fmt="png"):
if not isinstance(img, numpy.ndarray):
return None

retval, buf = cv2.imencode(".%s" % fmt, img)
if not retval:
return None

return buf, "image/%s" % fmt

renderers.append(render_opencv)
except ImportError:
pass

try:
from PIL import Image

def render_pil(img, fmt="png"):
if not callable(getattr(img, "save", None)):
return None

output = StringIO.StringIO()
img.save(output, format=fmt)
contents = output.getvalue()
output.close()

return contents, "image/%s" % fmt

renderers.append(render_pil)
except ImportError:
pass


class VisualRecord(object):
def __init__(self, title="", imgs=None, footnotes="", fmt="png"):
self.title = title
self.fmt = fmt

if imgs is None:
imgs = []

self.imgs = imgs

if not isinstance(imgs, (list, tuple, set, frozenset)):
self.imgs = [self.imgs]

self.footnotes = footnotes

def render_images(self):
rendered = []

for img in self.imgs:
for renderer in renderers:
# Trying renderers we have one by one
res = renderer(img, self.fmt)

if res is None:
continue
else:
rendered.append(res)
break

return "".join(
Template('<img src="data:$mime;base64,$data" />').substitute({
"data": base64.b64encode(data),
"mime": mime
}) for data, mime in rendered)

def render_footnotes(self):
if not self.footnotes:
return ""

return Template("<pre>$footnotes</pre>").substitute({
"footnotes": self.footnotes
})

def __str__(self):
t = Template(
"""
<h4>$title</h4>
$imgs
$footnotes
<hr/>""")

return t.substitute({
"title": self.title,
"imgs": self.render_images(),
"footnotes": self.render_footnotes()
})

0 comments on commit 28682d5

Please sign in to comment.
You can’t perform that action at this time.