From 0f45059bf7eb1ff628616b9bc1a332522fb4cff0 Mon Sep 17 00:00:00 2001 From: Austin Blanton Date: Fri, 18 Mar 2016 11:51:31 -0400 Subject: [PATCH] Add some landmarks HTML visualizations --- scripts/brpy/html_viz.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/scripts/brpy/html_viz.py b/scripts/brpy/html_viz.py index b99bc5b..5b2f90d 100644 --- a/scripts/brpy/html_viz.py +++ b/scripts/brpy/html_viz.py @@ -8,12 +8,46 @@ and host them as long as the relative paths remain the same on ya serva. from PIL import Image +LMSIZE = 8 + +def landmarks_on_crop(landmarks, x, y, width, height, imname, maxheight=None, color='green'): + ''' + Generates an HTML string that shows landmarks within a given cropped image. + When two landmarked crops are put next to each other, they will be inline. To make each crop its own line, wrap it in a div. + ''' + html = _bbcrop(x, y, width, height, imname, maxheight) + if not maxheight: + maxheight = height + ratio = float(maxheight) / height + print(ratio) + if len(landmarks) > 0 and len(landmarks[0]) != 3: + landmarks = [ lm + (color,) for lm in landmarks ] + for lmx,lmy,col in landmarks: + html += landmark((lmx-x)*ratio - (LMSIZE/2), (lmy-y)*ratio - (LMSIZE/2), col) + html += '' + return html + +def landmark_img(x, y, img, color='green'): + html = '
' + html += '' % img + html += landmark(x,y,color) + html += '
' + return html + +def landmark(x, y, color='green'): + return '
'.format(y,x,color,LMSIZE) + def crop_to_bb(x, y, width, height, imname, maxheight=None): ''' Generates an HTML string that crops to a given bounding box and resizes to maxheight pixels. A maxheight of None will keep the original size (default). When two crops are put next to each other, they will be inline. To make each crop its own line, wrap it in a div. ''' + html = _bbcrop(x, y, width, height, imname, maxheight) + html += '' + return html + +def _bbcrop(x, y, width, height, imname, maxheight): img = Image.open(imname) imwidth, imheight = img.size if not maxheight: @@ -23,9 +57,8 @@ def crop_to_bb(x, y, width, height, imname, maxheight=None): # image is cropped with div width/height + overflow:hidden, # resized with img height, # and positioned with img margin - html = '
' % (width*ratio, maxheight) + html = '
' % (width*ratio, maxheight) html += '' % (imname, imheight*ratio, y*ratio, x*ratio) - html += '
' return html def bbs_for_image(imname, bbs, maxheight=None, colors=None): -- libgit2 0.21.4