-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGenNonogram.py
112 lines (106 loc) · 3.67 KB
/
GenNonogram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from process import *
from convert import *
import argparse
from os import *
import cv2
def genNonogram(
fileName,
maxDim=80,
minDim=20,
toBW=False,
numColors="",
getResult=True,
getPuzzle=True,
):
print("Processing " + fileName + "...")
try:
processed = cv2.imread(fileName)
except:
print(fileName + "is not a path to a valid image")
sys.exit()
processed = pixelize(processed, maxDim, minDim, toBW)
processed = changeColors(processed, n=numColors)
basePath = fileName.split("/")
fileName = basePath[-1]
basePath = "".join(x + "/" for x in basePath[:-1])
print("Generating nonogram...")
if getResult:
print("Saving result...")
cv2.imwrite(basePath + "_Pic" + fileName, processed)
if getPuzzle:
print("Getting puzzle...")
bgColor = getBackgroundColor(processed)
clues = getClues(processed, bgColor)
height, width = processed.shape[:2]
product = drawGrid(clues, (width, height), bgColor)
product = drawClues(np.float32(product), clues, (width, height))
cv2.imwrite(basePath + "_Pix" + fileName, product)
print("Done!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Make a nonogram from a picture.")
parser.add_argument("image", nargs=1, help="path to image to be processed")
parser.add_argument(
"-d",
"--maxDim",
default=[80],
nargs=1,
type=int,
help="maximum no. of squares in any dimension in the nonogram (default = 80)",
)
parser.add_argument(
"--minDim",
default=[20],
nargs=1,
type=int,
help="minimum no. of squares in any dimension in the nonogram (default = 20). Takes priority over maxdim if in conflict.",
)
parser.add_argument(
"-b",
"--blackAndWhite",
action="count",
help="indicate the image should be converted to black and white",
)
parser.add_argument(
"-n",
"--numColor",
"--numColour",
nargs="?",
type=int,
default=0,
help="positive integer to denote the number of colors that should appear in your nonogram IN ADDITION to your background color.\nA 0 or negative integer means the algorithm will choose this number for you. \n (default = 0)",
)
parser.add_argument(
"-p",
"--getPuzzle",
dest="getPuzzle",
action="store_true",
help="boolean to indicate whether you want the puzzle with clues (add this to set to True, add -np / --no-getPuzzle to set to False)",
)
parser.add_argument(
"-np", "--no-getPuzzle", "--no-p", dest="getPuzzle", action="store_false"
)
parser.set_defaults(getPuzzle=True)
parser.add_argument(
"-r",
"--getResult",
dest="getResult",
action="store_true",
help="boolean to indicate whether you want the finished picture (add this to set to True, add -nr / --no-getResult to set to False)",
)
parser.add_argument(
"-nr", "--no-getResult", "--no-r", dest="getResult", action="store_false"
)
parser.set_defaults(getResult=True)
# parser.add_argument("-r","--getResult",nargs="?",type=bool,default=True,help="boolean to indicate whether you want the finished picture (default = True)")
# parser.add_argument('-p','--feature', dest='feature', action='store_true')
args = parser.parse_args()
print("arguments: " + str(args))
genNonogram(
args.image[0],
args.maxDim[0],
args.minDim[0],
bool(args.blackAndWhite),
(1 if args.blackAndWhite else args.numColor),
args.getResult,
args.getPuzzle,
)