EmbroidePy / pyembroidery

pyembroidery library for reading and writing a variety of embroidery formats.
MIT License
181 stars 33 forks source link

Cannot convert simple image to .pes file #170

Open christofidi opened 3 months ago

christofidi commented 3 months ago

Hello,

Thank you very much for open-sourcing this amazing library! I am trying to convert the outline of a simple image to a .pes file. For example, if I take a simple image of a dog, I am trying to detect the edges first and then convert the filtered image to a .pes file. If the problem is that I am trying to stitch only the edges, then I could use a fill-in stitch as well. This is the code that I am using, but the image appears with too many stitches all over the place (that do resemble the initial image in some sense):

def convert_image_to_pes(image_array, pes_file):

Create a new pattern

pattern = pyembroidery.EmbPattern()

# Iterate over the image array and add stitches
height, width = image_array.shape
for y in range(height):
    for x in range(width):
        if image_array[y, x] != 0:
            # Add a stitch at the current position with default color (black)
            pattern.add_stitch_absolute(pyembroidery.STITCH, x, y)

# Save the pattern to a PES file
pattern.write(pes_file)
print("Conversion successful!")

convert_image_to_pes(edges3, 'output.pes')

Do you maybe have any recommendations?

Cheers,

Georgia :)

tatarize commented 3 months ago

Filling in an area should likely be done with a much more advanced algorithm. For example, Eulerian Fill, which is a lot easier than something like Tatsumi.

While built into vpype (which is also fine to use) see this implementation: https://github.com/EmbroidePy/vpype-embroidery

See it with a combine image parser via this other utility: https://github.com/tatarize/vpype-vectrace

107616955-da69ca80-6c03-11eb-81bb-4150f2f4fb5d