Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit c11568b

Browse files
committed
update
1 parent 93dd564 commit c11568b

2 files changed

Lines changed: 107 additions & 15 deletions

File tree

generator.py

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
This Script is used to generate a custom code that can be passed to
44
the printer script.
55
6-
Each code consist of 4 segments, 3 four characters and 1 six character.
7-
for example "1100 1101 0100 001101"
6+
Each code can be summarized in 4 segments, 3 four character segments and 1 six
7+
character segment. For example "1100 1101 0100 001101"
88
"""
99

1010
import argparse
@@ -24,14 +24,52 @@
2424
"""
2525

2626

27-
# 4 segments in order, seperated by a space
28-
SEGMENTS = "1100 1101 0100 001101"
27+
def _getCode() -> tuple[str, str, str, str]:
28+
"""Get the code from the user
29+
"""
2930

31+
parser = argparse.ArgumentParser(description="Generate a code to pass to the printer.") # noqa
32+
parser.add_argument(
33+
"code",
34+
nargs="+",
35+
help="The code to generate with. Formated like this, '1100 1101 0100 001101'"
36+
)
3037

31-
def main():
32-
code = ""
38+
segments = parser.parse_args().code
39+
40+
# verify the segments
41+
if len(segments) != 4:
42+
print("Did not get 4 segments!")
43+
parser.exit()
44+
pass
45+
46+
for i, segment in enumerate(segments):
47+
# check if each segment only contains 1s and 0s
48+
if any(c not in "10" for c in segment):
49+
print(f"Segment {i+1} has invalid characters! It can only contain 1s and 0s.") # noqa
50+
parser.exit()
51+
pass
52+
53+
# check if the length is correct
54+
if i == 3:
55+
if len(segment) != 6:
56+
print("Segment 4 does not have 6 characters!")
57+
parser.exit()
58+
pass
59+
pass
60+
else:
61+
if len(segment) != 4:
62+
print(f"Segment {i+1} does not have 4 characters!")
63+
parser.exit()
64+
pass
65+
pass
66+
pass
67+
68+
return segments
3369

34-
seg1, seg2, seg3, seg4 = SEGMENTS.split(" ")
70+
71+
def main():
72+
seg1, seg2, seg3, seg4 = _getCode()
3573

3674
formatter = "01{seg1}{sep1}{seg2}{sep2}{seg3}{sep3}{seg4}{sep4}"
3775
formatter = partial(
@@ -42,38 +80,39 @@ def main():
4280
seg4=seg4
4381
)
4482

45-
code += formatter(
83+
formattedCode = ""
84+
formattedCode += formatter(
4685
sep1="10",
4786
sep2="10",
4887
sep3="10",
4988
sep4="01",
5089
)
51-
code += formatter(
90+
formattedCode += formatter(
5291
sep1="10",
5392
sep2="01",
5493
sep3="01",
5594
sep4="10",
5695
)
57-
code += formatter(
96+
formattedCode += formatter(
5897
sep1="01",
5998
sep2="10",
6099
sep3="01",
61100
sep4="01",
62101
)
63-
code += formatter(
102+
formattedCode += formatter(
64103
sep1="01",
65104
sep2="01",
66105
sep3="01",
67106
sep4="01",
68107
)
69-
code += formatter(
108+
formattedCode += formatter(
70109
sep1="01",
71110
sep2="01",
72111
sep3="10",
73112
sep4="10",
74113
)
75114

76-
print(code)
115+
print(formattedCode)
77116
pass
78117

79118

printer.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import argparse
2+
import os
3+
import pathlib
14
from dataclasses import dataclass
5+
26
from PIL import Image, ImageDraw
37

48
TEST_CODE = "01110010110110010010001101010111001011010101000100110110011100011101100100010011010101110001110101010001001101010111000111010101001000110110" # noqa
@@ -12,16 +16,64 @@
1216
CODON_WIDTH_TOLERANCE = 0.5
1317

1418

19+
class _PrinterArgs(argparse.Namespace):
20+
code: str = ""
21+
output: pathlib.Path = None
22+
pass
23+
24+
1525
@dataclass
1626
class _Codon(object):
1727
color: str
1828
startAngle: int
1929
endAngle: int
30+
pass
31+
32+
33+
def _getArgs() -> _PrinterArgs:
34+
parser = argparse.ArgumentParser(description="Create a printable barcode.")
35+
parser.add_argument(
36+
"code",
37+
help="A 140 character code to make a barcode with."
38+
)
39+
40+
parser.add_argument(
41+
"-o", "--output",
42+
type=pathlib.Path,
43+
help="The path of the output image. By default it outputs to 'output/customBarcode.png'" # noqa
44+
)
45+
46+
args = parser.parse_args(namespace=_PrinterArgs())
47+
48+
# verify the input code
49+
if len(args.code) != 140:
50+
print("The input code is not 140 character!")
51+
parser.exit()
52+
pass
53+
54+
if any(c not in "10" for c in args.code):
55+
print("The input code has invalid characters! It can only have 1s and 0s.")
56+
parser.exit()
57+
pass
58+
59+
return args
2060

2161

2262
def main():
23-
inputData = TEST_CODE
63+
args = _getArgs()
64+
65+
# Create the output directory if necessary
66+
outputPath = args.output
67+
if outputPath is None:
68+
outputPath = pathlib.Path("output/customBarcode.png")
69+
os.makedirs(outputPath.parent, exist_ok=True)
70+
pass
71+
else:
72+
os.makedirs(outputPath.parent, exist_ok=True)
73+
pass
2474

75+
# generate a list of codons
76+
inputData = args.code
2577
angleSize = 360 / len(inputData)
2678

2779
code: list[_Codon] = []
@@ -54,6 +106,7 @@ def main():
54106
i += codonWidth
55107
pass
56108

109+
# create the image
57110
with Image.new("1", (IMAGE_SIZE, IMAGE_SIZE), color=1) as im:
58111
draw = ImageDraw.Draw(im)
59112

@@ -70,7 +123,7 @@ def main():
70123
)
71124
pass
72125

73-
with open("output\\testImage.png", mode="wb") as outFile:
126+
with open(outputPath, mode="wb") as outFile:
74127
im.save(outFile, dpi=(DPI, DPI))
75128
pass
76129
pass

0 commit comments

Comments
 (0)