Skip to content

Commit

Permalink
Add flag to crop sprite animations
Browse files Browse the repository at this point in the history
  • Loading branch information
ExcaliburZero committed Oct 19, 2023
1 parent c35b7e5 commit 0935870
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cbpickaxe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
A library for data mining the game Cassette Beasts.
"""
from .animation import Animation, Frame, FrameTag
from .animation import Animation, Frame, FrameTag, Box
from .hoylake import Hoylake
from .item import Item
from .misc_types import Color
Expand All @@ -11,6 +11,7 @@

__all__ = [
"Animation",
"Box",
"Frame",
"FrameTag",
"Hoylake",
Expand Down
51 changes: 50 additions & 1 deletion cbpickaxe_scripts/generate_monster_animations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pylint: disable=missing-module-docstring,missing-function-docstring,missing-class-docstring
from typing import DefaultDict, List
from typing import DefaultDict, List, Optional

import argparse
import collections
Expand Down Expand Up @@ -28,6 +28,7 @@ def main(argv: List[str]) -> int:
],
)
parser.add_argument("--output_directory", required=True)
parser.add_argument("--crop", default=False, action="store_true")

args = parser.parse_args(argv)

Expand Down Expand Up @@ -70,7 +71,9 @@ def main(argv: List[str]) -> int:
for animation_name in animation:
_, frames = animation[animation_name]

furthest_extent: Optional[cbp.Box] = None
images = []
cropped_images = []
for frame in frames:
box = (
frame.box.x,
Expand All @@ -79,6 +82,31 @@ def main(argv: List[str]) -> int:
frame.box.y + frame.box.height,
)
cropped_image = source_image.crop(box)
cropped_images.append(cropped_image)

content_box = cropped_image.getbbox()
assert content_box is not None

furthest_extent = expand_box_to_include(
furthest_extent,
cbp.Box(
content_box[0],
content_box[1],
content_box[2] - content_box[0],
content_box[3] - content_box[1],
),
)

assert furthest_extent is not None

for image in cropped_images:
box = (
furthest_extent.x,
furthest_extent.y,
furthest_extent.x + furthest_extent.width,
furthest_extent.y + furthest_extent.height,
)
cropped_image = image.crop(box) if args.crop else image
images.append(cropped_image)

animation_filepath = (
Expand All @@ -99,5 +127,26 @@ def main(argv: List[str]) -> int:
return SUCCESS


def expand_box_to_include(box_a: Optional[cbp.Box], box_b: cbp.Box) -> cbp.Box:
if box_a is None:
return box_b

left = min(box_a.x, box_b.x)
top = min(box_a.y, box_a.y)

right = max(box_a.x + box_a.width, box_b.x + box_b.width)
bottom = max(box_a.y + box_a.height, box_b.y + box_b.height)

assert left < right
assert top < bottom

return cbp.Box(
left,
top,
right - left,
bottom - top,
)


def main_without_args() -> int:
return main(sys.argv[1:])

0 comments on commit 0935870

Please sign in to comment.