EmbroidePy / pyembroidery

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

Any way to export 'realistic' stitch view? #153

Closed cvick32 closed 1 year ago

cvick32 commented 1 year ago

Currently if I write a pattern out as a PNG it renders with stitches.

I was wondering if there's a way to render the PNG with a realistic, "stitched-out" look.

Thanks

tatarize commented 1 year ago

Currently no. But, a couple ways come to mind. Basically you sort of want to do a value gradient. Which means you vary the color slightly over the course of the line. There's more and more things you can do with that but that's the biggest thing you can do. Just vary the color so it's lightest at the middle and sort of dark at the ends. I double-checked the code a friend wrote for Embroidermodder's Embroidery-Viewer and implemented the idea more or less in the same sort of PNG thing as is used for this project last month for a side project.

Basically when doing a linear gradient for value, all you need to do is keep the RGB in the same proportions and multiply them all by the same scaling factor. If you take each component and you multiply them by say 0.9 you get a color that is 10% darker. You can use this trick to draw the lines with each part of the line getting a slightly different (darker or lighter) color over the course of the line. You tend to want darker at the ends where it goes into the fabric and lighter towards the center. And you need to add in a slight tweak when dealing with very dark colors or black (since 0,0,0 doesn't really get darker or lighter).

That said you can go all out and do 3d rendering of the overlapping thread or draw the sort of strands spiraling around in a sort of candycane effect of darker and lighter colors. You'll get some diminishing returns with that, but you could certainly do more and more to try to make it look better. But, the easiest and biggest get would just be drawing gradient lines.

I think my code was something like:

def gradient(v):
    if v <= 0.05:
        return ((v - 0) * (1 / 0.05)) * (1.0 - 0.5) + 0.5
    if v <= 0.5:
        return ((v - 0.05) * (1 / 0.45)) * (0.5 - 1.0) + 1.0
    if v <= 0.9:
        return ((v - 0.5) * (1 / 0.40)) * (1.0 - 0.5) + 0.5
    return ((v - .9) * (1 / 0.1)) * (0.5 - 1.0) + 1.0

For the gradient and then just fed that some of the line points. At a minimum it sort of gives you a better understanding of where line segments are, but it's pretty doable. I had somewhat considered porting it into pyembroidery but it's somewhat outside the scope. It's not hard to hand it that stuff as an option though. And a lot of folks do use that to visualize patterns and it does that pretty effectively.

tatarize commented 1 year ago

I had a branch of this that I wasn't really finished with but the idea was generally just that, add in gradient fancyview for more of a realistic stitch output. Though my token had expired or whatnot so it didn't get pushed sooner. I'd probably still need to hook some stuff up, but generally that's the idea behind giving it a bit more realistic stitches view.

cvick32 commented 1 year ago

Cool, thanks for the information.

For my use case, I need to let users layout things on a page, so I figured the best bet would be to just go through InkStitch.

If anyone needs this functionality, I have a pull request here for adding this feature in InkStitch.

Thanks again for your help.

tatarize commented 1 year ago

Yeah, it's a bit outside the sphere of what's relevant to this particular project. I'll likely add some of this stuff but over time since there's no pressing need for it, but it is mostly done.