Worked on additional tools

This commit is contained in:
Lukas Wölfer
2020-06-12 03:58:42 +02:00
parent 9a38c60488
commit 6565792030
6 changed files with 249 additions and 19 deletions

View File

@@ -41,27 +41,30 @@ def adjust_squares(
if not adjustment:
adjustment = Adjustment(w=10, h=10)
high_speed = False
def _adjustment_step(keycode: int) -> None:
def _adjustment_step(keycode: int, high_speed: bool) -> None:
assert adjustment is not None
x_keys = {81: -1, 83: +1, 104: -10, 115: +10}
y_keys = {82: -1, 84: +1, 116: -10, 110: +10}
x_keys = {104: -1, 115: +1}
y_keys = {116: -1, 110: +1}
w_keys = {97: -1, 117: +1}
h_keys = {111: -1, 101: +1}
dx_keys = {59: -1, 112: +1}
dy_keys = {44: -1, 46: +1}
high_speed_fac = 10
cur_high_speed_fac = high_speed_fac if high_speed else 1
if keycode in x_keys:
adjustment.x += x_keys[keycode]
adjustment.x += x_keys[keycode] * cur_high_speed_fac
elif keycode in y_keys:
adjustment.y += y_keys[keycode]
adjustment.y += y_keys[keycode] * cur_high_speed_fac
elif keycode in w_keys:
adjustment.w += w_keys[keycode]
adjustment.w += w_keys[keycode] * cur_high_speed_fac
elif keycode in h_keys:
adjustment.h += h_keys[keycode]
adjustment.h += h_keys[keycode] * cur_high_speed_fac
elif keycode in dx_keys:
adjustment.dx += dx_keys[keycode]
adjustment.dx += dx_keys[keycode] * cur_high_speed_fac
elif keycode in dy_keys:
adjustment.dy += dy_keys[keycode]
adjustment.dy += dy_keys[keycode] * cur_high_speed_fac
while True:
working_image = image.copy()
@@ -75,7 +78,10 @@ def adjust_squares(
print(keycode)
if keycode == 27:
break
_adjustment_step(keycode)
if keycode == 229:
high_speed = not high_speed
continue
_adjustment_step(keycode, high_speed)
cv2.destroyWindow("Window")
return adjustment

View File

@@ -99,10 +99,66 @@ def _save_adjustments(zip_file: zipfile.ZipFile, conf: Configuration) -> None:
adjustments = {}
adjustments[FIELD_ADJUSTMENT_KEY] = dataclasses.asdict(conf.field_adjustment)
adjustments[BORDER_ADJUSTMENT_KEY] = dataclasses.asdict(conf.border_adjustment)
zip_file.writestr(
ADJUSTMENT_FILE_NAME, json.dumps(adjustment),
adjustments[GOAL_ADJUSTMENT_KEY] = dataclasses.asdict(conf.goal_adjustment)
adjustments[BUNKER_ADJUSTMENT_KEY] = dataclasses.asdict(conf.bunker_adjustment)
adjustments[HUA_ADJUSTMENT_KEY] = dataclasses.asdict(conf.hua_adjustment)
adjustments[SPECIAL_BUTTON_ADJUSTMENT_KEY] = dataclasses.asdict(
conf.special_button_adjustment
)
print(adjustments)
zip_file.writestr(
ADJUSTMENT_FILE_NAME, json.dumps(adjustments),
)
def _save_special_images(zip_file: zipfile.ZipFile, conf: Configuration) -> None:
def _save_special_image(
zip_file: zipfile.ZipFile, images: List[np.ndarray], directory: str
) -> None:
for index, image in enumerate(images):
fd, myfile = tempfile.mkstemp(suffix=f".{PICTURE_EXTENSION}")
cv2.imwrite(myfile, image)
file_name = ""
zip_file.write(
myfile, arcname=f"{directory}/{index:03}.{PICTURE_EXTENSION}"
)
_save_special_image(zip_file, conf.card_border, CARD_BORDER_DIRECTORY)
_save_special_image(zip_file, conf.empty_card, EMPTY_CARD_DIRECTORY)
_save_special_image(zip_file, conf.green_card, GREEN_CARD_DIRECTORY)
_save_special_image(zip_file, conf.card_back, CARD_BACK_DIRECTORY)
def _generate_special_button_filename(
state: ButtonState, special_card: board.SpecialCard
) -> str:
state_char_map = {
ButtonState.normal: "n",
ButtonState.greyed: "g",
ButtonState.shiny: "s",
}
special_card_char_map = {
board.SpecialCard.Fa: "f",
board.SpecialCard.Zhong: "z",
board.SpecialCard.Bai: "b",
}
return f"{state_char_map[state]}{special_card_char_map[special_card]}"
def _save_special_button_images(
zip_file: zipfile.ZipFile,
special_button_images: List[Tuple[ButtonState, board.SpecialCard, np.ndarray]],
):
for index, (state, card, image) in enumerate(special_button_images):
fd, myfile = tempfile.mkstemp(suffix=f".{PICTURE_EXTENSION}")
cv2.imwrite(myfile, image)
file_name = ""
zip_file.write(
myfile,
arcname=f"{SPECIAL_BUTTON_DIRECTORY}/"
f"{_generate_special_button_filename(state,card)}"
f"{index:03}.{PICTURE_EXTENSION}",
)
def save(conf: Configuration, filename: str) -> None:
@@ -112,7 +168,8 @@ def save(conf: Configuration, filename: str) -> None:
with zipfile.ZipFile(zip_stream, "w") as zip_file:
_save_adjustments(zip_file, conf)
_save_catalogue(zip_file, conf.catalogue)
# TODO: Save card_borders and emtpy_card and green_card and special_buttons and card_back
_save_special_images(zip_file, conf)
_save_special_button_images(zip_file, conf.special_buttons)
with open(filename, "wb") as zip_archive:
zip_archive.write(zip_stream.getvalue())