Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #137 from kelltom/Fix-sprite-bank-variant-algorithm
Browse files Browse the repository at this point in the history
Fixed Sprite Scraper bank variant algorithm
  • Loading branch information
kelltom authored Feb 13, 2023
2 parents 03ca3fe + 1370a20 commit e106721
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/utilities/sprite_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def search_and_download(self, search_string: str, **kwargs):
nl = "\n"
notify_callback(f"Success: {img_names[i]} sprite saved.{nl if image_type != 2 else ''}")
if image_type in {1, 2}:
cropped_img = self.__crop_image(downloaded_img)
cropped_img = self.__bankify_image(downloaded_img)
cv2.imwrite(f"{filepath}_bank.png", cropped_img)
notify_callback(f"Success: {img_names[i]} bank sprite saved.\n")

Expand All @@ -128,22 +128,27 @@ def format_args(self, string: str) -> List[str]:
# Strip whitespace and replace spaces with underscores
return [word.strip().replace(" ", "_").capitalize() for word in string.split(",")]

def __crop_image(self, image: cv2.Mat) -> cv2.Mat:
def __bankify_image(self, image: cv2.Mat) -> cv2.Mat:
"""
Makes the top of the image transparent. This is used to crop out stack numbers in bank sprites.
Converts a sprite into an image that is suitable for image searching within a bank interface.
This function centers the image in a 36x32 frame, and deletes some pixels at the top of the image to
remove the stack number.
Args:
image: The image to crop.
Returns:
The cropped image.
The bankified image.
"""
BANK_SLOT_HALF_HEIGHT = 16
IMG_MAX_HEIGHT = 28
height, _, _ = image.shape
# Crop out stack numbers
crop_amt = int((height - BANK_SLOT_HALF_HEIGHT) / 2) if height > BANK_SLOT_HALF_HEIGHT else 0
if height >= IMG_MAX_HEIGHT:
crop_amt += 1 # Crop an additional pixel if the image is very tall
image[:crop_amt, :] = 0 # Set the top pixels to transparent
height, width = image.shape[:2]
max_height, max_width = 32, 36

if height > max_height or width > max_width:
print("Warning: Image is already larger than bank slot. This sprite is unlikely to be relevant for bank functions.")
return image

height_diff = max_height - height
width_diff = max_width - width
image = cv2.copyMakeBorder(image, height_diff // 2, height_diff // 2, width_diff // 2, width_diff // 2, cv2.BORDER_CONSTANT, value=0)
image[:9, :] = 0
return image

def __download_image(self, url: str) -> cv2.Mat:
Expand Down

0 comments on commit e106721

Please sign in to comment.