Skip to content

Commit 89de09d

Browse files
committed
added better support for template masks. letting you scale and rotate
1 parent 42a00fa commit 89de09d

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

data/steps_mask.png

188 Bytes
Loading

src/pixelsort/__main__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def main() -> None:
3434
help="threshold for contrast. [-1.0, 1.0] Default: 1.0",
3535
default=1.0,
3636
)
37-
threshold_template_group.add_argument("--template_path", type=str, help="path to template image")
37+
template_group = threshold_template_group.add_argument_group("Template Options")
38+
template_group.add_argument("--template_path", type=str, help="path to template image")
39+
template_group.add_argument("--template_scale", type=float, help="scale up and down the template", default=1.0)
40+
template_group.add_argument("--template_angle", type=float, help="angle of template", default=0)
3841

3942
# optional arguments
4043
parser.add_argument(
@@ -79,14 +82,16 @@ def main() -> None:
7982
angle = args.angle
8083
threshold = args.threshold
8184
template_path = args.template_path
85+
template_scale = args.template_scale
86+
template_angle = args.template_angle
8287
sort_brightest = args.sort_brightest
8388
reversed_direction = args.reverse_sorting
8489
output_path = args.output
8590

8691
# Manually check for mutual exclusive group
8792
if image_path is None and sys.stdin.isatty():
8893
parser.print_usage(sys.stderr)
89-
parser.error("one of the arguments --image_path stdin is required")
94+
parser.error("one of the arguments --image_path [stdin] is required")
9095

9196
sys.exit(2)
9297

@@ -116,7 +121,8 @@ def main() -> None:
116121
sys.exit(-1)
117122

118123
template = cv2.imread(str(template_path))
119-
create_mask = partial(image.create_template_mask, template)
124+
template = image.scale_image(template, template_scale)
125+
create_mask = partial(image.create_template_mask, template, template_angle-angle)
120126
else:
121127
create_mask = partial(image.create_contrast_mask, threshold)
122128

src/pixelsort/image.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def create_contrast_mask(threshold: float, image: np.ndarray) -> np.ndarray:
126126
return mask
127127

128128

129-
def create_template_mask(template: np.ndarray, image: np.ndarray) -> np.ndarray:
129+
def create_template_mask(template: np.ndarray, angle: float, image: np.ndarray) -> np.ndarray:
130130
"""Create a mask for the given image using the template
131131
132132
Args:
@@ -138,14 +138,17 @@ def create_template_mask(template: np.ndarray, image: np.ndarray) -> np.ndarray:
138138
"""
139139
# remove bg color channels
140140
template = template[:, :, 0]
141+
# Rotate Tile
142+
template = scipy.ndimage.rotate(template, -angle, mode="constant")
143+
141144
# Tile the template to the size of the image
142145
horizontal_tiles = int(image.shape[1] / template.shape[1]) + 1
143146
vertical_tiles = int(image.shape[0] / template.shape[0]) + 1
144147
mask = np.tile(template, (vertical_tiles, horizontal_tiles))
145148

146149
# Crop the mask to the size of the image
147150
mask = mask[: image.shape[0], : image.shape[1]]
148-
151+
149152
return mask
150153

151154

@@ -182,5 +185,11 @@ def show_image(image: np.ndarray) -> None:
182185
break
183186
cv2.destroyAllWindows()
184187

185-
def output_image(image: np.ndarray) -> None:
186-
pass
188+
def scale_image(image: np.ndarray, scale: float) -> np.ndarray:
189+
height, width, _ = image.shape
190+
width *= scale
191+
height *= scale
192+
193+
dim = (int(width), int(height))
194+
195+
return cv2.resize(image, dim, interpolation=cv2.INTER_NEAREST)

0 commit comments

Comments
 (0)