38 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from PIL import Image
 | 
						|
import random
 | 
						|
 | 
						|
# Load the GIF
 | 
						|
gif_path = "./garfield-garfield-meme-ezgif.com-added-text.gif"
 | 
						|
gif = Image.open(gif_path)
 | 
						|
 | 
						|
# Function to randomize the palette
 | 
						|
def randomize_palette(palette):
 | 
						|
    # Convert palette (a flat list) to a list of (R, G, B) tuples
 | 
						|
    colors = [(palette[i], palette[i + 1], palette[i + 2]) for i in range(0, len(palette), 3)]
 | 
						|
    # Shuffle the colors
 | 
						|
    random.shuffle(colors)
 | 
						|
    # Flatten the list back to the original format
 | 
						|
    randomized_palette = [val for color in colors for val in color]
 | 
						|
    return randomized_palette
 | 
						|
 | 
						|
# Extract frames and modify frame 21
 | 
						|
frames = []
 | 
						|
for i in range(gif.n_frames):
 | 
						|
    gif.seek(i)
 | 
						|
    new_frame = gif.convert("P", palette=Image.ADAPTIVE)  # Convert to palette mode
 | 
						|
    
 | 
						|
    if i == 20:
 | 
						|
        # Get the palette of the frame and randomize it
 | 
						|
        palette = new_frame.getpalette()
 | 
						|
        randomized_palette = randomize_palette(palette)
 | 
						|
        # Apply the randomized palette to the frame
 | 
						|
        new_frame.putpalette(randomized_palette)
 | 
						|
 | 
						|
    # Append the modified (or unmodified) frame
 | 
						|
    frames.append(new_frame)
 | 
						|
 | 
						|
# Save the modified GIF
 | 
						|
output_path = "./randomized_palette_gif.gif"
 | 
						|
frames[0].save(output_path, save_all=True, append_images=frames[1:], loop=0, duration=gif.info['duration'])
 | 
						|
print(f"Modified GIF with randomized palette saved as {output_path}")
 |