def update_background_image(frames, frame_index):
if frames: # Check if frames are available
frame = frames[frame_index % len(frames)] # Use modulo to loop the frame index
canvas.itemconfig(background_image, image=frame)
canvas.image = frame # Maintain reference
window.after(100, update_background_image, frames, frame_index + 1)
def load_and_process_gif(file_path, blur=False, resize=None):
frames = []
count = 0
try:
img = Image.open(file_path)
while True:
img.seek(count)
img_frame = img.copy().convert('RGBA') # Convert frame to RGBA mode
if resize:
img_frame = img_frame.resize(resize, Image.Resampling.LANCZOS) # Use high-quality resampling
if blur:
img_frame = img_frame.filter(ImageFilter.GaussianBlur(radius=2)) # Apply blur
frame = ImageTk.PhotoImage(img_frame)
frames.append(frame)
count += 1
except EOFError:
pass # End of GIF
except Exception as e:
print("Error loading GIF:", e)
return frames
frames = load_and_process_gif(relative_to_assets('background.gif'), blur=True, resize=(752, 423))
background_image = canvas.create_image(376, 211.5, image=None) # Center position
window.after(0, update_background_image, frames, 0) # Start the animation