Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gameboy color background #24

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 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,10 @@ 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 +219,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.
16 changes: 10 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,11 @@ 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 +99,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 +114,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
Loading