how to stop path closing for kvg definition kanji character

24 views Asked by At

I have this kanji character definition

<kanji id="kvg:kanji_02e95">
<g id="kvg:02e95" kvg:element="⺕" kvg:variant="true" kvg:original="彑" kvg:radical="tradit">
    <path id="kvg:02e95-s1" kvg:type="㇕" d="M22.62,22.19c3.12,1.18,6.1,1.24,9.49,1.02c15.13-0.97,31.51-2.34,43.51-3.22c3.24-0.24,5.38,1.25,5,5.17c-1.07,11.19-1.5,35.19-1.6,54.58C79.01,82.8,79,85.74,79,88.5"/>
    <path id="kvg:02e95-s2" kvg:type="㇐" d="M12.25,54.09c3.67,1.22,7.46,0.95,11.23,0.64c18.02-1.48,43.3-3.07,63.02-4.52c4.04-0.3,8.12-0.43,12.12,0.18"/>
    <path id="kvg:02e95-s3" kvg:type="㇐c" d="M22.88,86.88c3.25,1.24,6.03,1.29,9.22,0.93c15.03-1.69,33.02-3.48,46.14-3.95"/>
</g>
</kanji>

Which i render to a png format. This is the element kvg:element="⺕", but for S1 character I endup having closed path and I should have only strokes. I tried putting a 'z' at the end and tried defining the path to have stroke-width='1', used 'L' after 'M' and at the end with the beginning coordinates etc. Coordinates at the end do not match the beginning. Strangely enough this is happening to only one of the three strokes adn it happens to some of the strokes in some of the kanji characters, I have about 6000 of them. This is the code I used:

root = ET.fromstring(xml_data)

# Loop through all elements in the XML
for element in root.iter():
    if 'kanji' in element.tag:
        try:
            # Debug print to check the character code extraction
            character_code = element.attrib.get('id')
            if character_code is None:
                print("Skipping element without 'id' attribute.")
                continue

            character_code = character_code.split('_')[-1]
            print(f"Character code: {character_code}")

            # Extract SVG data from the kanji element with added width and height attributes
            svg_data = f'<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100 "  stroke_width="1">{ET.tostring(element, encoding="unicode")}</svg>'

            # Add the path element to the SVG data
            #svg_data_with_path = svg_data.replace('</kanji>', ET.tostring(path, encoding="unicode") + '</kanji>')

            # Render SVG to PNG using cairosvg
            #png_data = cairosvg.svg2png(bytestring=svg_data_with_path.encode('utf-8'))
            
            png_data = cairosvg.svg2png(bytestring=svg_data.encode('utf-8'))

            # Convert PNG data to a PIL Image
            image = Image.open(io.BytesIO(png_data))

            # Save the image with the character code as the filename under the specified directory
            file_path = os.path.join(output_directory, f'{character_code}.png')
            #image.save(file_path)
            
            # Display the image in the notebook
            display(image)

            # Display the file path
            print(f"Image saved to: {file_path}")
        except Exception as e:
            # Print any exception that occurred during image creation
            print(f"Error creating image: {e}")

This is the image it renders

enter image description here

0

There are 0 answers