Skip to content

Instantly share code, notes, and snippets.

@uhidontkno
Created February 10, 2024 05:10
Show Gist options
  • Save uhidontkno/d0eee5f60ffc39bfd63ef33bacb3c0da to your computer and use it in GitHub Desktop.
Save uhidontkno/d0eee5f60ffc39bfd63ef33bacb3c0da to your computer and use it in GitHub Desktop.
from PIL import Image
def img2hex(gif_path, output_path):
try:
img = Image.open(gif_path)
except FileNotFoundError:
print(f"Error: File not found - {gif_path}")
return
except Exception as e:
print(f"Error: {e}")
return
frames = []
try:
while True:
frames.append(img.convert("RGBA").tobytes())
img.seek(img.tell() + 1)
except EOFError:
pass
hex_values = []
for frame in frames:
hex_values.extend(["{:02x}{:02x}{:02x}".format(frame[i], frame[i+1], frame[i+2]) for i in range(0, len(frame), 4)])
hex_values.append("\n")
with open(output_path, "w") as outfile:
outfile.write(" ".join(hex_values).strip())
gif_path = "input.gif"
output_path = "out.txt"
img2hex(gif_path, output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment