-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
tool_show_boxes.py
157 lines (132 loc) · 4.81 KB
/
tool_show_boxes.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from __future__ import annotations
import argparse
import logging
logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
from PIL import Image, ImageDraw
from depixlib.helpers import (
check_file,
check_color
)
from depixlib.functions import (
dropEmptyRectangleMatches,
findGeometricMatchesForSingleResults,
findRectangleMatches,
findRectangleSizeOccurences,
findSameColorSubRectangles,
removeMootColorRectangles,
splitSingleMatchAndMultipleMatches,
writeAverageMatchToImage,
writeFirstMatchToImage
)
from depixlib.LoadedImage import LoadedImage
from depixlib.Rectangle import Rectangle
def parse_args() -> argparse.Namespace:
usage = """
note:
The pixelated rectangle must be cut out to only include the pixelated rectangles.
The pattern search image is generally a screenshot of a De Bruijn sequence of expected characters,
made on a machine with the same editor and text size as the original screenshot that was pixelated.
"""
parser = argparse.ArgumentParser(
description="This command recovers passwords from pixelized screenshots.",
epilog=usage
)
parser.add_argument(
"-p",
"--pixelimage",
help="path to image with pixelated rectangle",
required=True,
default=argparse.SUPPRESS,
type=check_file,
metavar="PATH"
)
parser.add_argument(
"-s",
"--searchimage",
help="path to image with patterns to search",
required=True,
default=argparse.SUPPRESS,
type=check_file,
metavar="PATH",
)
parser.add_argument(
"-a",
"--averagetype",
help="type of RGB average to use",
default="gammacorrected",
choices=["gammacorrected", "linear"],
metavar="TYPE",
)
parser.add_argument(
"-b",
"--backgroundcolor",
help="original editor background color in format r,g,b (color to ignore)",
default=None,
type=check_color,
metavar="RGB"
)
parser.add_argument(
"-o",
"--outputimage",
help="path to output image",
default="output.png",
metavar="PATH",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
pixelatedImagePath = args.pixelimage
searchImagePath = args.searchimage
editorBackgroundColor: tuple[int, int, int] | None = args.backgroundcolor
averageType = args.averagetype
logging.info("Loading pixelated image from %s" % pixelatedImagePath)
pixelatedImage = LoadedImage(pixelatedImagePath)
unpixelatedOutputImage = pixelatedImage.getCopyOfLoadedPILImage()
logging.info("Loading search image from %s" % searchImagePath)
searchImage = LoadedImage(searchImagePath)
logging.info("Finding color rectangles from pixelated space")
# fill coordinates here if not cut out
pixelatedRectange = Rectangle(
(0, 0), (pixelatedImage.width - 1, pixelatedImage.height - 1)
)
pixelatedSubRectanges = findSameColorSubRectangles(
pixelatedImage, pixelatedRectange
)
logging.info("Found %s same color rectangles" % len(pixelatedSubRectanges))
pixelatedSubRectanges = removeMootColorRectangles(
pixelatedSubRectanges, editorBackgroundColor
)
logging.info("%s rectangles left after moot filter" % len(pixelatedSubRectanges))
rectangeSizeOccurences = findRectangleSizeOccurences(pixelatedSubRectanges)
logging.info("Found %s different rectangle sizes" % len(rectangeSizeOccurences))
if len(rectangeSizeOccurences) > max(
10, pixelatedRectange.width * pixelatedRectange.height * 0.01
):
logging.warning(
"Too many variants on block size. Re-cropping the image might help."
)
logging.info("Finding matches in search image")
rectangleMatches = findRectangleMatches(
rectangeSizeOccurences, pixelatedSubRectanges, searchImage, averageType
)
logging.info("Removing blocks with no matches")
pixelatedSubRectanges = dropEmptyRectangleMatches(
rectangleMatches, pixelatedSubRectanges
)
# Enhance like in the movies
enhance = 3
image = Image.open(pixelatedImagePath)
enhancedImage = image.resize((image.width*enhance, image.height*enhance))
draw = ImageDraw.Draw(enhancedImage)
# Show crappy box detector output
for box in pixelatedSubRectanges:
draw.line([
(box.x*enhance, box.y*enhance),
(((box.x+box.width)*enhance)-enhance, box.y*enhance ),
(((box.x+box.width)*enhance)-enhance, ((box.y+box.height)*enhance)-enhance),
(box.x*enhance, ((box.y+box.height)*enhance) - enhance),
(box.x*enhance, box.y*enhance)
], width=1, fill="red")
enhancedImage.show()
if __name__ == "__main__":
main()