Sure! Below is a Python script that uses the Pillow library to create a 64x64 grayscale GIF of little bubbles moving upwards. The GIF loops seamlessly, featuring white bubbles, a gray background, and two horizontal lines in the darkest gray.
Requirements
Before running the script, ensure you have the Pillow library installed. You can install it using pip:
pip install Pillow
Python Script
from PIL import Image, ImageDraw
import random
# Configuration
WIDTH, HEIGHT = 64, 64
NUM_FRAMES = 16 # Total number of frames in the GIF
MOVE_PER_FRAME = 4 # Pixels to move upwards each frame
NUM_BUBBLES = 8 # Number of bubbles
BUBBLE_MIN_RADIUS = 2
BUBBLE_MAX_RADIUS = 5
# Colors (Grayscale)
COLOR_BACKGROUND = 128 # Gray background
COLOR_LINES = 64 # Darkest gray for lines
COLOR_BUBBLE = 255 # White bubbles
# Horizontal lines positions
LINE_Y_POSITIONS = [HEIGHT // 4, 3 * HEIGHT // 4]
# Bubble class to hold properties of each bubble
class Bubble:
def __init__(self, x, y, radius):
self.x = x
self.y = y
self.radius = radius
def move_up(self, pixels):
self.y -= pixels
if self.y + self.radius < 0:
self.y += HEIGHT + 2 * self.radius # Wrap around
def create_bubbles(num_bubbles):
bubbles = []
for _ in range(num_bubbles):
radius = random.randint(BUBBLE_MIN_RADIUS, BUBBLE_MAX_RADIUS)
x = random.randint(radius, WIDTH - radius)
y = random.randint(0, HEIGHT - radius)
bubbles.append(Bubble(x, y, radius))
return bubbles
def create_frame(bubbles):
# Create a new grayscale image
img = Image.new('L', (WIDTH, HEIGHT), color=COLOR_BACKGROUND)
draw = ImageDraw.Draw(img)
# Draw horizontal lines
for y in LINE_Y_POSITIONS:
draw.line([(0, y), (WIDTH, y)], fill=COLOR_LINES)
# Draw bubbles
for bubble in bubbles:
upper_left = (bubble.x - bubble.radius, bubble.y - bubble.radius)
lower_right = (bubble.x + bubble.radius, bubble.y + bubble.radius)
draw.ellipse([upper_left, lower_right], fill=COLOR_BUBBLE)
return img
def main():
# Initialize bubbles
bubbles = create_bubbles(NUM_BUBBLES)
frames = []
for frame_number in range(NUM_FRAMES):
# Create frame
frame = create_frame(bubbles)
frames.append(frame)
# Move bubbles for next frame
for bubble in bubbles:
bubble.move_up(MOVE_PER_FRAME)
# Save as GIF
# To ensure a perfect loop, the last frame should transition back to the first seamlessly
# PIL handles this when the frames loop correctly
frames[0].save(
'bubbles.gif',
save_all=True,
append_images=frames[1:],
duration=100, # Duration of each frame in milliseconds
loop=0, # 0 means loop indefinitely
disposal=2 # Restore to background before next frame
)
print("GIF saved as 'bubbles.gif'.")
if __name__ == "__main__":
main()
How It Works
-
Configuration: The script sets up the image dimensions, number of frames, movement per frame, number of bubbles, bubble sizes, and colors.
-
Bubble Class: A Bubble class holds the position and size of each bubble. It includes a method to move the bubble upwards and wrap it around to the bottom when it goes off the top edge.
-
Creating Bubbles: The create_bubbles function initializes a list of bubbles with random positions and sizes within the image boundaries.
-
Creating Frames: The create_frame function draws the background, two horizontal lines, and all the bubbles for the current frame.
-
Animation Loop: In the main function, for each frame, it creates the frame image, appends it to the frames list, and updates the positions of the bubbles for the next frame.
-
Saving the GIF: Finally, the frames are saved as an animated GIF named bubbles.gif. The duration parameter sets how long each frame is displayed (in milliseconds), and loop=0 makes the GIF loop indefinitely.
Sample Output
Running the script will generate a bubbles.gif file in the same directory. Here's an example of what the GIF might look like:
[image: Sample Bubbles GIF] open (Note: Replace with an actual image if available)
Customization
- Number of Bubbles: Adjust the
NUM_BUBBLES variable to have more or fewer bubbles.
- Bubble Sizes: Change
BUBBLE_MIN_RADIUS and BUBBLE_MAX_RADIUS to vary bubble sizes.
- Movement Speed: Modify
MOVE_PER_FRAME to make bubbles move faster or slower.
- Frame Count: Increasing
NUM_FRAMES can make the animation smoother but will increase the GIF file size.
Feel free to tweak these parameters to better suit your preferences!