Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. import cv2
  2. from os import listdir
  3. from os.path import join, exists
  4. from os import makedirs
  5. from lxml import etree
  6. import png
  7. import xml.etree.ElementTree as ET
  8. import io
  9. import base64
  10.  
  11. def recursivityFilling(element, file):
  12.     for subEl in element:
  13.         if subEl.tag == '{http://www.w3.org/2000/svg}g': # Check if leaf or node
  14.             recursivityFilling(subEl, file)
  15.         else:
  16.             folder = subEl.attrib['id'].lower() # The id contains the same name as the folder
  17.             folder = folder.replace(" ", "") # But might contain some whitespaces that we remove
  18.             label = cv2.imread(join(INP_FOLDER, folder, file), 0)
  19.             if a is not None:
  20.                 with io.BytesIO() as buffer:
  21.                     pngW = png.Writer(width=label.shape[1], height=label.shape[0], greyscale=True, bitdepth=1)
  22.                     pngW.write(buffer, label>200)
  23.                     pngData = base64.b64encode(buffer.getvalue())
  24.                 imgAttribute = 'src="data:image/png;base64, %s"' % pngData
  25.                 subEl.set('xlink:href', imgAttribute)
  26.  
  27. list_img = listdir(join(INP_FOLDER, 'raw')) # reference of existing files.
  28. list_img.remove('.directory')
  29.  
  30. svg_path = join(INP_FOLDER, 'svg') # creating the results folders
  31. if not exists(svg_path):
  32.     makedirs(svg_path)
  33.    
  34.    
  35. for f in list_img:
  36.     tree = ET.parse('fundus.svg') # The reference SVG that we use to get the expected hierarchy.
  37.     root = tree.getroot()
  38.     for child in root:
  39.         recursivityFilling(child, f)
  40.     tree.write(join(INP_FOLDER, 'svg', f[:-3]+'svg')) # Write the svg with the same name as the reference image.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement