EmbroidePy / pyembroidery

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

PNGWriter automatically align to center the Pattern #100

Closed alcpereira closed 3 years ago

alcpereira commented 3 years ago

I'm working on a DST merging and translating project. To visually see if it's working, I usually export the Pattern into a PNG file.

I don't know if it is intended or not but the PNGWriter automatically align to center the Pattern. We could add a setting for this.

tatarize commented 3 years ago

Most embroidery is set to align to the center of the pattern. The PNG writer is no exception. It gets the bounds. Translates the pattern to the minimum x and y coords and creates a PngBuffer for the length and width.

    extends = pattern.bounds()
    pattern.translate(-extends[0], -extends[1])
    width = int(extends[2] - extends[0])
    height = int(extends[3] - extends[1])

Settings-wise the writer has background and linewidth. So what specifically would this modification entail? A lot of designs fall into negative regions since embroidery starts at 0,0 which is very often made to be the center of the design. So other than moving the it to be centered in the PNG, what exactly would this require?


I see how using the PNG to visualize the position would be problematic. But, I don't see any trivial solution. I wouldn't want to import Pillow/PIL and only wrote the PNGs since I coded it up with pure built-ins. I suppose I could add a static text monofont and draw a guide on the image. But, most embroidery extends into negative regions and PNGs can't unless I move them to the center of the pattern.

tatarize commented 3 years ago

I spent a few hours and cooked up a guide for it.

https://github.com/EmbroidePy/pyembroidery/tree/tatarize-pngguides

You can try the branch. It should tell you the location of various bits. I had to craft up the fonts by extracting the glyph information and doing alpha-blending with the background image, when writing the static values. Since I'm not going to make it dependant on anything. I had only cooked up the png writer because there was a good recipe for writing them.

Also, I moved all the PyEmbroidery stuff into the EmbPattern and turned the PyEmbroidery into a shell. This allows me to use the filename as an init argument and various settings as kwargs without upsetting anything. And adds .write() to the EmbPattern itself. This is part of #90. To smooth out and make the API more obvious.

from pyembroidery import EmbPattern
EmbPattern('cat.pes').write("test.png", guides=True)

This also allows things like:

pattern = EmbPattern('cat.pes') + EmbPattern('duck.pes')

Which previously would have been a bit harder to write, API-wise.

tatarize commented 3 years ago

This seemed to have settled the problem.