| import xml.etree.ElementTree as ET |
| import statistics |
|
|
| import argparse |
|
|
|
|
| def remove_low_opacity_paths(svg_file_path, output_file_path, opacity_delta=0.2): |
| try: |
| |
| tree = ET.parse(svg_file_path) |
| namespace = "http://www.w3.org/2000/svg" |
| ET.register_namespace("", namespace) |
|
|
| root = tree.getroot() |
| root.set('version', '1.1') |
|
|
| paths = root.findall('.//{http://www.w3.org/2000/svg}path') |
| |
| opacity_values = [] |
| for path in paths: |
| opacity = path.get("stroke-opacity") |
| if opacity is not None: |
| opacity_values.append(float(opacity)) |
|
|
| |
| median_opacity = statistics.median(opacity_values) + opacity_delta |
|
|
| |
| paths_to_remove = [] |
| for path in paths: |
| opacity = path.get('stroke-opacity') |
| if opacity is not None and float(opacity) < median_opacity: |
| paths_to_remove.append(path) |
|
|
| |
| for path in paths_to_remove: |
| path.set('stroke-opacity', '0') |
|
|
| print(f"n_path: {len(paths)}, " |
| f"opacity_thresh: {median_opacity}, " |
| f"n_path_to_remove: {len(set(paths_to_remove))}.") |
|
|
| |
| tree.write(output_file_path, encoding='utf-8', xml_declaration=True, default_namespace="") |
| |
| |
| except Exception as e: |
| print(f"An error occurred: {str(e)}") |
|
|
|
|
| if __name__ == '__main__': |
| """ |
| python process_svg.py -save ./workdir/xx.svg -tar ./workdir/xx.svg |
| """ |
| parser = argparse.ArgumentParser(description="vary style painterly rendering") |
| parser.add_argument("-tar", "--target_file", |
| default="", type=str, |
| help="the path of SVG file place.") |
| parser.add_argument("-save", "--save_path", |
| default="", type=str, |
| help="the path of processed SVG file place.") |
| parser.add_argument("-od", "--opacity_delta", |
| default=0.1, type=float) |
| args = parser.parse_args() |
|
|
| remove_low_opacity_paths(args.target_file, args.save_path, float(args.opacity_delta)) |
|
|