Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# It seems that these transformation are used by InkScape but
# not by pdf2cairo (which is here used to convert PDF->SVG)
def is_black(path):
return any("style" in path and color in path["style"]
for color in [r"rgb(0%,0%,0%)", r"#000000"])
is_handwritten = is_handwritten or is_black
def remove_unit(string):
unit = re.findall("[a-z]+", string)
if unit:
return string.replace(unit[-1], "")
strokes = []
paths, properties, svg_attributes = svg2paths(
filename,
return_svg_attributes=True)
width = float(remove_unit(svg_attributes["width"]))
height = float(remove_unit(svg_attributes["height"]))
for path, property_ in zip(paths, properties):
if not is_handwritten(property_):
continue
polynomials = [segment.poly() for segment in path]
t = np.linspace(0, 1, 10)
polygon = np.concatenate([segment(t) for segment in polynomials])
stroke = np.array([polygon.real, polygon.imag]).transpose()
if "transform" in property_:
transform = property_["transform"]
if "matrix" in transform:
parameters_str = re.findall(r"matrix\((.+)\)", transform)[0]
parameters = np.array(
filename_filelist_svg_fold = os.path.join(root_folder, 'filelist_fold_%d.txt' % (idx_fold))
if os.path.exists(filename_filelist_svg_fold):
print('{}-{} exists, skipping'.format(datetime.now(), filename_filelist_svg_fold))
continue
with open(filename_filelist_svg_fold, 'w') as filelist_svg_fold_file:
for filename in filelist_svg_fold:
filelist_svg_fold_file.write('%s\n' % (filename))
idx_h5 = 0
idx = 0
filename_filelist_h5 = os.path.join(root_folder, 'fold_%d_files%s.txt' % (idx_fold, tag_aug))
with open(filename_filelist_h5, 'w') as filelist_h5_file:
for idx_file, filename in enumerate(filelist_svg_fold):
filename_svg = os.path.join(folder_svg, filename)
try:
paths, attributes = svg2paths(filename_svg)
except:
filelist_svg_failed.append(filename_svg)
print('{}-Failed to parse {}!'.format(datetime.now(), filename_svg))
continue
points_array = np.zeros(shape=(args.point_num, 3), dtype=np.float32)
normals_array = np.zeros(shape=(args.point_num, 3), dtype=np.float32)
path = Path()
for p in paths:
p_non_empty = Path()
for segment in p:
if segment.length() > 0:
p_non_empty.append(segment)
if len(p_non_empty) != 0:
path.append(p_non_empty)