terriko / pyknit

A set of tools for knitters to create charts and eventually more.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Draw chart symbols correctly

terriko opened this issue · comments

I started with code to draw chart symbols down in the bottom of the chart file:

# draw symbol for each cell

But they don't currently work and I haven't debugged why. If anyone else wants to play with it before I get to it, go right ahead!

I'll work on this!

It was indeed a font problem. I went for Courier New as that seems to be a good and universal monospace font. Here's the code I have so far for Chart.py:

# draw symbol for each cell
    fnt = ImageFont.truetype("Courier New.ttf", 40)
    for i, stitch in enumerate(stitch_array):
        draw.text(
            ((cell_width + 1) * i, 3), stitch, font=fnt, fill=(255, 255, 255, 255)
        )
    return chart_image

I tested it with this code:

pattern = "k1 p4 k1 p4 k kfb r ssk k2tog"
legend = {
        "k": " ",
        "p": ".",
        "kfb": "\\/",
        "ssk": "\\",
        "k2tog": "k2tog",
        "r": "r"
    }

pattern_parsed = pyknit.parse_written(pattern, legend)
print(pattern_parsed)

pattern_printed = pyknit.print_chart(pattern_parsed)
pattern_printed.show()

and it produced this image:
tmpnqe4m1zy

I don't know how knitting patterns should look so I could do with some pointers here.

  1. I presume you intentionally want it to print \/ and not \/? Is that correct?
  2. k2tog doesn't fit in the box at the moment. What would you like to do with that?

An example of input and desired output of print_chart would help me here. Thanks.

In a typical knitting chart, stitch symbols are a width that indicates either the starting or final size of the stitch. There's some sort of unspoken conventions about which ones are which, and I'm not sure I could articulate it (and different designers may prefer different things, hence why I'm starting with a legend rather than assuming a standard set of symbols)

  1. kfb is "knit front and back" and it takes a single stitch and turns it into two stitches. I've seen it represented a few ways and I was experimenting with how it worked. Probably the easiest thing to do for now is replace it in the legend with a V (looks like one stitch at the bottom and two at the top that way, and takes up one space). I have seen it listed as a two-space symbol too.
  2. k2tog is "knit two together" and normally would be represented as / (I probably had not set this because I was experimenting with what would happen if my symbol was too large). It's a decrease stitch (take two stitches, turn them into one) but because people find it easier to "read" what it looks like on the final knitting, it's basically always represented as a single-wide symbol.

So this chart would likely be supposed to look something like...

.... .... Vr\/

Which is kind of a nonsense chart by itself, but could make sense as part of a longer pattern.

My first try used standard keyboard symbols, but if you want to take a look at some more standard symbols, here's the list from the crafts americana website: https://www.craftyarncouncil.com/standards/knit-chart-symbols

Note that they use V to represent "slip stitch" and don't have an explicit kfb symbol, but it would likely be a v with a 2 over it, showing that you're increasing to two stitches.

I'm not sure what the best way to handle these in pyKnit is yet -- there are knitting symbol fonts but I didn't see one that looked like it had a license for redistribution with pyKnit, so we might want to import images/svg or draw them ourselves and find a way to specify 'use built-in symbol" in the legend.

And also, this is looking great. Even if we only use standard font symbols to start, that can get you a pretty large range of patterns into a state where we can chart them!

A thought: we are going to eventually want to make symbols that are more than one unit wide. We'll probably want the legend to have 3 parts, then: written instruction, symbol, width (possibly as start size/end size for calculating). It might be time to turn legend into some sort of more explicitly defined datatype.

Closed thanks to @ben-hampson ! I'm going to open a new issue for improving our default stitch dictionary.