Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image resize option added #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
logger.addHandler(fh)

logger.debug(VisualRecord(
"Hello from OpenCV", cv_image, "This is openCV image", fmt="png"))
"Hello from OpenCV", cv_image, "This is openCV image", fmt="png", size=(50, 50)))

logger.info(VisualRecord(
"Hello from PIL", pil_image, "This is PIL image", fmt="jpeg"))
"Hello from PIL", pil_image, "This is PIL image", fmt="jpeg", size=(100, 300)))

logger.info(VisualRecord(
"Hello from pylab", fig1, "This is PyLab graph", fmt="png"))
"Hello from pylab", fig1, "This is PyLab graph", fmt="png", size=(100, 300)))

logger.warning(
VisualRecord("Hello from all", [cv_image, pil_image, fig1],
fmt="png"))
fmt="png", size=(50, 50)))
19 changes: 14 additions & 5 deletions vlogging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import cv2
import numpy

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

if size and isinstance(size, tuple):
img = cv2.resize(img, size)
retval, buf = cv2.imencode(".%s" % fmt, img)
if not retval:
return None
Expand All @@ -28,11 +30,13 @@ def render_opencv(img, fmt="png"):
try:
from PIL import Image

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

output = StringIO()
if size and isinstance(size, tuple):
img = img.resize(size)
img.save(output, format=fmt)
contents = output.getvalue()
output.close()
Expand All @@ -46,11 +50,15 @@ def render_pil(img, fmt="png"):
try:
import pylab

def render_pylab(img, fmt="png"):
def render_pylab(img, fmt="png", size=None):
if not callable(getattr(img, "savefig", None)):
return None

output = StringIO()
if size and isinstance(size, tuple):
size = (size[0]/img.dpi, size[1]/img.dpi)
img.set_size_inches(size)

img.savefig(output, format=fmt)
contents = output.getvalue()
output.close()
Expand All @@ -63,9 +71,10 @@ def render_pylab(img, fmt="png"):


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

if imgs is None:
imgs = []
Expand All @@ -83,7 +92,7 @@ def render_images(self):
for img in self.imgs:
for renderer in renderers:
# Trying renderers we have one by one
res = renderer(img, self.fmt)
res = renderer(img, self.fmt, self.size)

if res is None:
continue
Expand Down