Skip to content

Commit

Permalink
Added gameboy color option
Browse files Browse the repository at this point in the history
  • Loading branch information
TomCasavant committed May 5, 2024
1 parent eab258d commit 714382b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
9 changes: 5 additions & 4 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def retry_mastodon_call(self, func, *args, retries=5, interval=10, **kwargs):
for _ in range(retries):
try:
return func(*args, **kwargs)
except RequestException as e:
except Exception as e:
print(f"Failure to execute {func.__name__}: {e}")
time.sleep(interval)
return False # Failed to execute
Expand Down Expand Up @@ -189,7 +189,7 @@ def run(self):
frames = self.gameboy.loop_until_stopped()
result = False
if frames >= 70:
result = self.gameboy.build_gif("gif_images")
result = self.gameboy.build_gif("gif_images", self.gameboy_config['gif_outline'])
else:
gif_dir = os.path.join(self.script_dir, "gif_images")
self.gameboy.empty_directory(gif_dir)
Expand All @@ -207,7 +207,7 @@ def run(self):
# Probably add a check here if generating a gif is enabled (so we don't
# have to generate one every single hour?)
try:
previous_frames = self.gameboy.get_recent_frames("screenshots", 25)
previous_frames = self.gameboy.get_recent_frames("screenshots", 25, self.gameboy_config['gif_outline'])
previous_media = self.retry_mastodon_call(
self.mastodon.media_post,
retries=5,
Expand All @@ -216,7 +216,8 @@ def run(self):
description="Video of the previous 45 frames",
)
media_ids = [media["id"], previous_media["id"]]
except BaseException:
except BaseException as e:
print(f"ERROR {e}")
media_ids = [media["id"]]

post = self.retry_mastodon_call(
Expand Down
3 changes: 2 additions & 1 deletion config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ poll_duration = 60

[gameboy]
rom = "Pokemon - Gold Version.gbc"
title = "Pokémon Gold"
title = "Pokémon Gold"
gif_outline="gameboy.png"
Binary file added gameboycolor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions gb.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def compare_frames(self, frame1, frame2):
print(f"Pixels: {changed_pixels} {percent}%")
return percent

def get_recent_frames(self, directory, num_frames=100):
def get_recent_frames(self, directory, num_frames=100, gif_outline='gameboy.png'):
"""Gets the most recent frames from a provided directory"""
script_dir = os.path.dirname(os.path.realpath(__file__))
screenshot_dir = os.path.join(script_dir, directory)
Expand All @@ -81,7 +81,7 @@ def get_recent_frames(self, directory, num_frames=100):
count += 1
shutil.copy(image, os.path.join(script_dir, "tmp", f"{count}.png"))

self.build_gif(os.path.join(script_dir, "tmp"), fps=5, output_name="test.mp4")
self.build_gif(os.path.join(script_dir, "tmp"), fps=5, output_name="test.mp4", gif_outline=gif_outline)
self.empty_directory(os.path.join(script_dir, "tmp"))
return os.path.join(script_dir, "test.mp4")

Expand All @@ -95,7 +95,7 @@ def empty_directory(self, directory):
for img in image_files:
os.remove(os.path.join(directory, img))

def build_gif(self, image_path, delete=True, fps=120, output_name="action.mp4"):
def build_gif(self, image_path, delete=True, fps=120, output_name="action.mp4", gif_outline="gameboy.png"):
"""Build a gif from a folder of images"""
# Get the directory of the current script
script_dir = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -110,12 +110,12 @@ def build_gif(self, image_path, delete=True, fps=120, output_name="action.mp4"):
for file in image_files:

gameboy_outline = Image.open(
os.path.join(script_dir, "gameboy.png")
os.path.join(script_dir, gif_outline)
).convert("RGB")
img = Image.open(os.path.join(gif_dir, file)).convert("RGB")
img = img.resize((181, 163))
img = img.resize((184, 170))
combined = gameboy_outline.copy()
combined.paste(img, (165, 151))
combined.paste(img, (159, 138)) #338, 308
combined.save(os.path.join(gif_dir, file))
images.append(os.path.join(gif_dir, file))

Expand Down

0 comments on commit 714382b

Please sign in to comment.