Skip to content

Instantly share code, notes, and snippets.

@penut85420
Last active October 14, 2023 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save penut85420/3e844ee8aad286eda269de108afd2479 to your computer and use it in GitHub Desktop.
Save penut85420/3e844ee8aad286eda269de108afd2479 to your computer and use it in GitHub Desktop.
Convert .gif or .png to material design style
# Made By ChatGPT: https://chat.openai.com/share/a38487f2-6cbe-4342-8ba3-29daee547f0f
import numpy as np
from PIL import Image, ImageSequence
def apply_material_design_simple(frame):
"""
Apply Material Design color scheme to a single frame (or image).
"""
# Convert the frame to a NumPy array
frame_array = np.array(frame.convert("RGBA"))
# Separate RGB and alpha channels
rgb, alpha = frame_array[:, :, :3], frame_array[:, :, 3]
# Define Material Design base colors in RGB
md_light = np.array([250, 250, 250])
md_dark = np.array([33, 33, 33])
# Calculate a blending factor based on the brightness
brightness = np.mean(rgb, axis=2) / 255.0
# Compute blended color
a = (1 - brightness)[:, :, np.newaxis] * md_light
b = brightness[:, :, np.newaxis] * md_dark
blended_rgb = a + b
# Reassemble the frame
blended_frame_array = np.zeros_like(frame_array)
blended_frame_array[:, :, :3] = blended_rgb.astype(np.uint8)
blended_frame_array[:, :, 3] = alpha
# Convert the NumPy array back to an image
blended_frame = Image.fromarray(blended_frame_array, "RGBA")
return blended_frame
# Example usage for GIF
def process_gif(input_path, output_path):
# Open the original GIF
input_gif = Image.open(input_path)
# Extract frames from the original GIF
frames = [frame.copy() for frame in ImageSequence.Iterator(input_gif)]
# Extract frame duration from the original GIF
frame_duration = input_gif.info.get("duration", 0)
# Apply Material Design colors and keep original frame rate
material_frames = [apply_material_design_simple(frame) for frame in frames]
# Save the new GIF
material_frames[0].save(
output_path,
save_all=True,
append_images=material_frames[1:],
loop=0,
duration=frame_duration,
)
# Example usage for PNG
def process_png(input_path, output_path):
# Open the original PNG
input_png = Image.open(input_path)
# Apply Material Design colors
material_png = apply_material_design_simple(input_png)
# Save the new PNG
material_png.save(output_path)
# Uncomment below lines to test the functions
process_gif("source.gif", "target.gif")
process_png("source.png", "target.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment