Sure! Below is a Python script that creates a looping GIF of your logo with a "bounce in" effect. This script uses the Pillow library for image manipulation.
Prerequisites
-
Python Installed: Ensure you have Python 3.x installed on your system.
-
Pillow Library: Install Pillow if you haven't already. You can install it using pip:
pip install Pillow
Script Overview
- Load the Logo: The script loads your logo from the specified path.
- Define Easing Function: Implements a bounce easing function (
ease_out_bounce) to create the bounce effect.
- Generate Frames: Creates multiple frames where the logo scales up from 0% to 100% size with the bounce effect.
- Save as GIF: Compiles all frames into a looping GIF.
Complete Python Script
from PIL import Image
import math
import os
def ease_out_bounce(t):
"""
Bounce easing function (ease out)
t: float between 0 and 1
returns: float between 0 and 1
"""
n1 = 7.5625
d1 = 2.75
if t < 1 / d1:
return n1 * t * t
elif t < 2 / d1:
t -= 1.5 / d1
return n1 * t * t + 0.75
elif t < 2.5 / d1:
t -= 2.25 / d1
return n1 * t * t + 0.9375
else:
t -= 2.625 / d1
return n1 * t * t + 0.984375
def create_bounce_in_gif(
logo_path,
output_path,
frame_size=(1024, 1024),
bg_color=(255, 255, 255, 0), # Transparent background
total_frames=30,
duration=100 # Duration per frame in milliseconds
):
"""
Creates a bouncing in GIF animation for a logo.
Args:
logo_path (str): Path to the logo image.
output_path (str): Path where the GIF will be saved.
frame_size (tuple): Size of the GIF frames (width, height).
bg_color (tuple): Background color as RGBA. (255,255,255,0) is transparent.
total_frames (int): Number of frames in the GIF.
duration (int): Duration of each frame in milliseconds.
"""
try:
logo = Image.open(logo_path).convert("RGBA")
except Exception as e:
print(f"Error loading logo image: {e}")
return
frames = []
for frame_number in range(total_frames):
t = frame_number / (total_frames - 1) # Normalize t to [0,1]
scale = ease_out_bounce(t) # Scale factor with bounce effect
# Clamp scale to [0,1]
scale = max(0, min(scale, 1))
# Calculate new size
new_width = int(logo.width * scale)
new_height = int(logo.height * scale)
# Resize logo
resized_logo = logo.resize((new_width, new_height), Image.ANTIALIAS)
# Create background
frame = Image.new("RGBA", frame_size, bg_color)
# Calculate position to center the logo
x = (frame_size[0] - new_width) // 2
y = (frame_size[1] - new_height) // 2
# Paste the resized logo onto the background
frame.paste(resized_logo, (x, y), resized_logo)
frames.append(frame)
if not frames:
print("No frames were created.")
return
# Save as GIF
try:
# Convert frames to palette-based (necessary for GIF)
frames_converted = []
for frame in frames:
# Convert to palette mode
frame_p = frame.convert('P', palette=Image.ADAPTIVE)
frames_converted.append(frame_p)
# Save the frames as an animated GIF
frames_converted[0].save(
output_path,
save_all=True,
append_images=frames_converted[1:],
optimize=True,
duration=duration,
loop=0,
transparency=0,
disposal=2
)
print(f"GIF saved successfully at {output_path}")
except Exception as e:
print(f"Error saving GIF: {e}")
if __name__ == "__main__":
# Define paths
logo_path = "/mnt/c/Users/Jeff/logo/logo.png" # Path to your logo
output_path = "/mnt/c/Users/Jeff/logo/logo_bounce.gif" # Output GIF path
# Ensure the logo exists
if not os.path.isfile(logo_path):
print(f"Logo file does not exist at path: {logo_path}")
else:
create_bounce_in_gif(
logo_path=logo_path,
output_path=output_path,
frame_size=(1024, 1024),
bg_color=(255, 255, 255, 0), # Transparent background
total_frames=30, # Adjust for smoother animation
duration=50 # Adjust frame duration (ms) as needed
)
Explanation of the Script
-
Easing Function (ease_out_bounce):
- Implements a typical "ease-out-bounce" easing function.
- Takes a normalized time
t between 0 and 1 and returns a scaling factor that simulates a bounce effect.
-
create_bounce_in_gif Function:
- Parameters:
logo_path: Path to your logo image.
output_path: Desired path for the output GIF.
frame_size: Size of each frame in the GIF (default is 1024x1024).
bg_color: Background color. (255, 255, 255, 0) represents a transparent background.
total_frames: Total number of frames in the GIF. More frames result in smoother animation.
duration: Duration of each frame in milliseconds.
- Process:
- Loads the logo image and ensures it's in RGBA mode (supports transparency).
- Iterates over the number of frames, calculating the appropriate scale for each frame using the easing function.
- Resizes the logo based on the current scale and centers it on a transparent background.
- Collects all frames into a list.
- Converts frames to a palette-based mode required for GIFs.
- Saves all frames as an animated GIF that loops indefinitely (
loop=0).
-
Main Execution Block:
- Sets the paths for the input logo and output GIF.
- Checks if the logo file exists before proceeding.
- Calls the
create_bounce_in_gif function with desired parameters.
Customization
Running the Script
-
Save the Script: Save the above script to a file, e.g., create_bounce_gif.py.
-
Run the Script:
python create_bounce_gif.py
-
Result: After running, the script will generate a logo_bounce.gif at the specified output_path with your logo animated with a bounce-in effect.
Notes
- Performance: GIFs can become large with high resolution and many frames. Adjust
total_frames and frame_size as needed to balance quality and file size.
- Transparency: Ensure your original logo has a transparent background (supports PNG with alpha channel) to maintain transparency in the GIF.
- Compatibility: Some platforms may handle GIF transparency differently. Test the output GIF to ensure it meets your expectations.
Feel free to modify and enhance the script according to your specific needs!