knitout_to_dat_python.dat_file_structure.knitout_to_dat_converter module

Dat_File class for creating Shima Seiki DAT files from knitout files.

This module provides comprehensive functionality for converting knitout files to Shima Seiki DAT format. It handles the complete conversion pipeline including knitout parsing, raster generation, run-length encoding, and DAT file creation. The implementation is based on the CMU Textile Lab’s knitout-to-dat.js functionality.

class Knitout_to_Dat_Converter(knitout: str, dat_filename: str, knitout_in_file: bool = True)[source]

Bases: object

A class for creating Shima Seiki DAT files from knitout files.

DAT files are encoded raster images containing knitting patterns and machine instructions. The format consists of a header, color palette, and run-length encoded pixel data. This class handles the complete conversion pipeline from knitout parsing through DAT file generation.

Parameters:
  • knitout (str)

  • dat_filename (str)

  • knitout_in_file (bool, default: True)

PALETTE_STR: str = 'ff 00 ff 00 ff 00 ff 00 6c 4a ff b4 99 90 80 cf 52 51 eb 00 fc b2 fc fc fc fc 64 d8 eb a0 90 73 9d 73 d8 eb ff b4 ac d7 d8 7f d8 90 ca d8 ae bc 80 9f ff dc fc c0 d8 fc 90 ff fd b4 00 a0 32 32 00 35 d8 d8 a8 c0 ff 99 b7 00 e2 c5 90 c0 90 90 4a 00 90 6d 00 00 66 33 85 99 78 ca b4 90 7d ff ff ff 7f 69 fa 81 fc ac 7f b2 b4 b4 b4 d4 ff 90 ff c0 c0 73 d8 a9 bf b4 ff 90 d8 b2 aa 00 d8 00 fb 90 81 9d 37 ac dd bf b9 3f ef d7 de fd fe 73 2f 8d fb ff fe ed 06 f5 ea ed ad 3d fc fa ef fd 66 8d 7f 7a 5f 79 9b 71 ff ee a8 ff 9f db f5 ff cd f3 e0 fe c8 79 73 1f bf e5 f3 f6 e0 de f0 cc 4b 64 40 a1 f7 1a e0 67 ff 64 f5 3f 97 ef 14 96 d7 67 b7 ee ba ea 6c bd 26 4e 64 2f bf 9f 7f f3 aa ff e6 bf 57 eb 06 fe 4f ed 6a ef 62 b7 dd cf 66 6b b2 7a 5a f7 9c 4c 96 9d 00 00 6e c8 00 64 00 00 ff ff 00 00 ff ff 24 89 67 b4 99 6c 80 90 91 ff eb 7c b4 76 6c 94 b4 d8 c8 90 ac 66 d8 73 7f b2 d8 eb 00 b4 ac c3 48 00 d8 6c a7 b4 8d 9a 60 7f 90 76 fc ff fc fc ff 90 eb 90 ff ff ca e9 d5 af 6c 6c 54 60 ff 66 bc a0 c5 ae cf ff b4 d8 89 70 c0 a5 99 66 c1 ad 7a d6 30 28 6c 48 8f 00 99 66 00 3f a3 64 d8 eb 7f b2 6c 90 d8 95 bf 6c cf cf 90 b2 d8 e5 6a d8 dd d8 b4 73 00 00 9d 96 fd 65 df 5a 9d ac f3 df f7 6e ff db ff fb fb ab 31 c7 fa af 6a af 03 9d fe ea 0c 9f de a7 f5 7d 00 c7 ff 67 bf 7f 7f 87 fc ce bf 2f 6f be ba fd f2 5f 2d df c8 7f 5b b5 77 6f 8f db 92 7e f0 5f ff 9d 40 ba f7 ec 6d fb 64 64 96 e3 c7 f7 d3 ff af 7f f5 f6 73 f7 b2 5a 5f 88 89 b7 bc fd 7f e9 7f 7e 2f fa 7c f7 03 a5 c7 ea fb 8d ff ff 79 5b 00 e7 8d 67 b9 ec 59 f7 00 bd 96 af 00 00 7d 64 00 00 00 00 ff ff ff ff 90 99 bd d8 99 b4 ff c0 db de 24 91 6c b2 48 63 fc fc c8 fc eb 00 48 b2 01 73 48 ac a0 6c eb e1 90 7f fc d8 e1 d8 f5 46 ff ff 90 75 b4 90 48 90 c0 cf c7 90 ff ff e9 e9 00 ed b4 d8 b4 b4 ff ff bc a0 b2 b7 c0 cf fc fc 99 99 cf b4 ff ff ff ff 03 ff 9c 91 d8 b4 a5 8f d2 bb 00 24 b9 0c 6c ac 00 73 6c 48 d8 95 bf 6c 90 90 cf b2 b4 e7 69 90 ad fc 6c 73 00 7f 49 00 fe fd a5 6f 7f ff 7b be ab 11 67 ff b9 55 9d 7f fb de 7f 7f 7f fb f0 93 fe fb eb bf ef 5d f7 fc 8a de ff 96 3a bd df bb f8 3d b0 cf 9e fe 5f fd f3 d9 ff 93 c8 bd aa 37 fd 81 7f be ff 7f f0 91 4b 4c 40 4b 67 ce ff a9 7d ff 64 d3 6f f7 b4 f7 ad cf fc e9 cd 7f 81 af 64 f7 51 f5 a4 7d df 3f cf f7 fd f9 7f df f0 4d 5f fb ff fb 4f df a9 f0 8a 45 ba 96 fc bd 09 b7 00 f2 00 00 00 00 00 64'

Hexadecimal string representation of the standard DAT file color palette.

Type:

str

PALETTE_BYTES = b'\xff\x00\xff\x00\xff\x00\xff\x00lJ\xff\xb4\x99\x90\x80\xcfRQ\xeb\x00\xfc\xb2\xfc\xfc\xfc\xfcd\xd8\xeb\xa0\x90s\x9ds\xd8\xeb\xff\xb4\xac\xd7\xd8\x7f\xd8\x90\xca\xd8\xae\xbc\x80\x9f\xff\xdc\xfc\xc0\xd8\xfc\x90\xff\xfd\xb4\x00\xa022\x005\xd8\xd8\xa8\xc0\xff\x99\xb7\x00\xe2\xc5\x90\xc0\x90\x90J\x00\x90m\x00\x00f3\x85\x99x\xca\xb4\x90}\xff\xff\xff\x7fi\xfa\x81\xfc\xac\x7f\xb2\xb4\xb4\xb4\xd4\xff\x90\xff\xc0\xc0s\xd8\xa9\xbf\xb4\xff\x90\xd8\xb2\xaa\x00\xd8\x00\xfb\x90\x81\x9d7\xac\xdd\xbf\xb9?\xef\xd7\xde\xfd\xfes/\x8d\xfb\xff\xfe\xed\x06\xf5\xea\xed\xad=\xfc\xfa\xef\xfdf\x8d\x7fz_y\x9bq\xff\xee\xa8\xff\x9f\xdb\xf5\xff\xcd\xf3\xe0\xfe\xc8ys\x1f\xbf\xe5\xf3\xf6\xe0\xde\xf0\xccKd@\xa1\xf7\x1a\xe0g\xffd\xf5?\x97\xef\x14\x96\xd7g\xb7\xee\xba\xeal\xbd&Nd/\xbf\x9f\x7f\xf3\xaa\xff\xe6\xbfW\xeb\x06\xfeO\xedj\xefb\xb7\xdd\xcffk\xb2zZ\xf7\x9cL\x96\x9d\x00\x00n\xc8\x00d\x00\x00\xff\xff\x00\x00\xff\xff$\x89g\xb4\x99l\x80\x90\x91\xff\xeb|\xb4vl\x94\xb4\xd8\xc8\x90\xacf\xd8s\x7f\xb2\xd8\xeb\x00\xb4\xac\xc3H\x00\xd8l\xa7\xb4\x8d\x9a`\x7f\x90v\xfc\xff\xfc\xfc\xff\x90\xeb\x90\xff\xff\xca\xe9\xd5\xafllT`\xfff\xbc\xa0\xc5\xae\xcf\xff\xb4\xd8\x89p\xc0\xa5\x99f\xc1\xadz\xd60(lH\x8f\x00\x99f\x00?\xa3d\xd8\xeb\x7f\xb2l\x90\xd8\x95\xbfl\xcf\xcf\x90\xb2\xd8\xe5j\xd8\xdd\xd8\xb4s\x00\x00\x9d\x96\xfde\xdfZ\x9d\xac\xf3\xdf\xf7n\xff\xdb\xff\xfb\xfb\xab1\xc7\xfa\xafj\xaf\x03\x9d\xfe\xea\x0c\x9f\xde\xa7\xf5}\x00\xc7\xffg\xbf\x7f\x7f\x87\xfc\xce\xbf/o\xbe\xba\xfd\xf2_-\xdf\xc8\x7f[\xb5wo\x8f\xdb\x92~\xf0_\xff\x9d@\xba\xf7\xecm\xfbdd\x96\xe3\xc7\xf7\xd3\xff\xaf\x7f\xf5\xf6s\xf7\xb2Z_\x88\x89\xb7\xbc\xfd\x7f\xe9\x7f~/\xfa|\xf7\x03\xa5\xc7\xea\xfb\x8d\xff\xffy[\x00\xe7\x8dg\xb9\xecY\xf7\x00\xbd\x96\xaf\x00\x00}d\x00\x00\x00\x00\xff\xff\xff\xff\x90\x99\xbd\xd8\x99\xb4\xff\xc0\xdb\xde$\x91l\xb2Hc\xfc\xfc\xc8\xfc\xeb\x00H\xb2\x01sH\xac\xa0l\xeb\xe1\x90\x7f\xfc\xd8\xe1\xd8\xf5F\xff\xff\x90u\xb4\x90H\x90\xc0\xcf\xc7\x90\xff\xff\xe9\xe9\x00\xed\xb4\xd8\xb4\xb4\xff\xff\xbc\xa0\xb2\xb7\xc0\xcf\xfc\xfc\x99\x99\xcf\xb4\xff\xff\xff\xff\x03\xff\x9c\x91\xd8\xb4\xa5\x8f\xd2\xbb\x00$\xb9\x0cl\xac\x00slH\xd8\x95\xbfl\x90\x90\xcf\xb2\xb4\xe7i\x90\xad\xfcls\x00\x7fI\x00\xfe\xfd\xa5o\x7f\xff{\xbe\xab\x11g\xff\xb9U\x9d\x7f\xfb\xde\x7f\x7f\x7f\xfb\xf0\x93\xfe\xfb\xeb\xbf\xef]\xf7\xfc\x8a\xde\xff\x96:\xbd\xdf\xbb\xf8=\xb0\xcf\x9e\xfe_\xfd\xf3\xd9\xff\x93\xc8\xbd\xaa7\xfd\x81\x7f\xbe\xff\x7f\xf0\x91KL@Kg\xce\xff\xa9}\xffd\xd3o\xf7\xb4\xf7\xad\xcf\xfc\xe9\xcd\x7f\x81\xafd\xf7Q\xf5\xa4}\xdf?\xcf\xf7\xfd\xf9\x7f\xdf\xf0M_\xfb\xff\xfbO\xdf\xa9\xf0\x8aE\xba\x96\xfc\xbd\t\xb7\x00\xf2\x00\x00\x00\x00\x00d'

Binary representation of the DAT file color palette.

Type:

bytes

HEADER_SIZE = 512

Size of the DAT file header in bytes.

Type:

int

PALETTE_SIZE = 1024

Size of the palette section in bytes (768 bytes palette + padding to 1024 bytes).

Type:

int

DATA_OFFSET = 1536

Offset where the run-length encoded data begins in the DAT file.

Type:

int

__init__(knitout: str, dat_filename: str, knitout_in_file: bool = True)[source]

Initialize a Dat_File instance.

Parameters:
  • knitout (str) – Path to the input knitout file or knitout content string.

  • dat_filename (str) – Name for the output DAT file.

  • knitout_in_file (bool, optional) – Whether knitout parameter is a file path (True) or content string (False). Defaults to True.

Raises:
  • ValueError – If palette data is not the expected 768 bytes.

  • FileNotFoundError – If knitout file is specified but not found.

  • RuntimeError – If knitting range is outside the specified needle bed range when using Keep position.

knitout: str

The knitout file path or content string.

Type:

str

knitout_is_file: bool

Whether knitout parameter represents a file path.

Type:

bool

dat_filename: str

Output DAT filename.

Type:

str

knitout_lines: list[Knitout_Line]

Parsed knitout lines from the input.

Type:

list[Knitout_Line]

knitout_executer: Knitout_Executer_With_Kickbacks

Executor for processing knitout instructions with kickback support.

Type:

Knitout_Executer_With_Kickbacks

position_offset: int

Offset for positioning the pattern on the needle bed.

Type:

int

raster_data: list[list[int]]

2D array of pixel values representing the complete DAT raster.

Type:

list[list[int]]

property dat_width: int

Get the width in pixels of the dat file.

Returns:

The width in pixels of the dat file. Returns 0 if no raster data exists.

Return type:

int

property dat_height: int

Get the height in pixels of the dat file.

Returns:

The height in pixels of the dat file.

Return type:

int

property leftmost_slot: int

Get the minimum needle position of operations in the knitout code.

Returns:

The minimum needle position of operations in the knitout code. If the knitout never uses a needle position, this will be set to 0.

Return type:

int

property rightmost_slot: int

Get the maximum needle position of operations in the knitout code.

Returns:

The maximum needle position of operations in the knitout code. If the knitout never uses a needle position, this will be set to 0.

Return type:

int

property slot_range: tuple[int, int]

Get the leftmost and rightmost needle slots of the knitout process.

Returns:

The leftmost and rightmost needle slots of the knitout process.

Return type:

tuple[int, int]

property knitout_header: Knitting_Machine_Header

Get the Knitting Machine Header parsed from the given knitout.

Returns:

The Knitting Machine Header parsed from the given knitout. Default header values are set if a header value is not explicitly defined.

Return type:

Knitting_Machine_Header

property machine_specification: Knitting_Machine_Specification

Get the Knitting Machine Specification parsed from the given knitout header.

Returns:

The Knitting Machine Specification parsed from the given knitout header.

Return type:

Knitting_Machine_Specification

property specified_carrier_count: int

Get the number of carriers specified for the machine.

Returns:

The number of carriers specified for the machine given the knitout file header or default values. Defaults to 10 carriers.

Return type:

int

property specified_position: Knitting_Position

Get the position on the bed to knit on.

Returns:

The position on the bed to knit on given the knitout file header or default values. Defaults to Right side of bed.

Return type:

Knitting_Position

property specified_needle_bed_width: int

Get the count of needles on each bed.

Returns:

The count of needles on each bed given the knitout file header or default values. Defaults to 540 needles.

Return type:

int

property specified_gauge: int

Get the gauge of the knitting machine.

Returns:

The gauge of the knitting machine (needles per inch) given the knitout file header or default values. Defaults to 15 needles per inch.

Return type:

int

get_dat_header_info() dict[str, int][source]

Get current header information.

Returns:

Dictionary with header information including min_slot, max_slot, position_offset, and pattern_width.

Return type:

dict[str, int]

property knitting_width: int

Get the width of the range of needles used by the knitting operations.

Returns:

The width of the range of needles used in by the knitting operations. Returns 0 if rightmost_slot is not greater than leftmost_slot.

Return type:

int

create_raster_from_knitout(pattern_vertical_buffer: int = 5, pattern_horizontal_buffer: int = 4, option_horizontal_buffer: int = 10) None[source]

Create raster data from the parsed knitout instructions.

Generates the complete raster representation of the knitout pattern including startup sequences, main pattern operations, ending sequences, and appropriate spacing and buffers.

Parameters:
  • pattern_vertical_buffer (int, optional) – Vertical spacing buffer around the pattern. Defaults to 5.

  • pattern_horizontal_buffer (int, optional) – Horizontal spacing buffer around the pattern. Defaults to 4.

  • option_horizontal_buffer (int, optional) – Horizontal spacing buffer around option lines. Defaults to 10.

Return type:

None

run_length_encode() list[int][source]

Run-length encode the raster data into index-length pairs.

Compresses the raster data using run-length encoding where consecutive pixels of the same color are represented as color-index and run-length pairs. This is the standard compression method used in DAT files.

Returns:

List of alternating color indices and run lengths.

Return type:

list[int]

Raises:

ValueError – If no raster data exists to encode.

create_dat_header() bytearray[source]

Create the DAT file header.

Generates the binary header section of the DAT file including dimensions, magic numbers, and other metadata required by the Shima Seiki DAT format specification.

Returns:

Header as a bytearray of HEADER_SIZE bytes.

Return type:

bytearray

static create_palette_section() bytearray[source]

Create the palette section of the DAT file.

Generates the color palette section using the standard DAT file palette data, padded to the required PALETTE_SIZE.

Returns:

Palette section as a bytearray (padded to PALETTE_SIZE).

Return type:

bytearray

write_dat_file() None[source]

Write the complete DAT file to disk.

Creates the complete binary DAT file including header, palette, and run-length encoded raster data. Outputs file information including size and dimensions upon successful completion.

Raises:

ValueError – If no raster data exists to write.

Return type:

None

create_empty_raster(width: int, height: int) None[source]

Create an empty raster filled with background color (0).

Parameters:
  • width (int) – Width of the raster in pixels.

  • height (int) – Height of the raster in pixels.

Return type:

None

create_empty_dat(width: int = 50, height: int = 10) None[source]

Create a simple empty DAT file for testing purposes.

Creates a minimal DAT file with the specified dimensions filled with background color for testing and validation purposes.

Parameters:
  • width (int, optional) – Width of the raster. Defaults to 50.

  • height (int, optional) – Height of the raster. Defaults to 10.

Return type:

None

process_knitout_to_dat() None[source]

Complete workflow: parse knitout file and create DAT file.

Executes the complete conversion pipeline from knitout parsing through DAT file generation, including raster creation and file writing with progress reporting.

Return type:

None