Create an Animated GIF

Introduction

Pillow is a Python Image Library. You will need it for this project.

Here is how to test if Pillow is installed and what version is installed...

import PIL PIL.__version__

if it is not installed or needs an upgrade...

python3 -m pip install --upgrade pip python3 -m pip install --upgrade Pillow

Project #0

Write a description of the following...

Project #1

create an animated GIF file.

For example
image mising

To see an animated GIF click HERE

Project #2

Next to the dot, write the dot's x,y coordinates. They should change as the dot moves.

Project #3

Create some related images in a directory. Create an animated GIF from the images.

For example, in a drawing program create images of a stick figure walking.

Note: all of the images should be the same size to create the GIF.

project #4

The animation in project #1 is kinda jerky. Smooth it out.

Sample Code To Get You Started

#!/usr/bin/python3 # ========================================================= # create an animated GIF file # this code does the work in Cartesian coordinates and # converts to image/window coordinates to draw # ========================================================= import math from PIL import Image, ImageDraw # --------------------------------------------------------- # ---- convert Cartesian coordinates to image/window # ---- coordinates # ---- Note: # ---- 1. The Cartesian coordinate's origin (0,0) is in # ---- the center of the image/window # ---- 2. The image/window coordinate's origin (0,0) is # ---- in the upper-left corner of the image # --------------------------------------------------------- def coord_convert(cx,cy,image_size): <add code here> return (ix,iy) # ---------------------------------------------------------- # create a new image for the GIF # ---------------------------------------------------------- def new_image(angle,radius,image_size): # ---- create a new image object image = Image.new('RGB', image_size, 'white') # ---- create a draw object draw = ImageDraw.Draw(image) # ---- draw x axis <add code here> draw.line(xy,fill='black',width=2) # ---- draw y axis <add code here> draw.line(xy,fill='black',width=2) # ---- draw a large circle <add code here> draw.ellipse(xy, outline='black', fill=None, width=3) # ---- draw a dot on the circle at coordinates x,y # ---- and perpendicular lines to the axes rad = math.radians(angle) x = radius * math.cos(rad) y = radius * math.sin(rad) # ---- draw a horizontal a line between the dot # ---- and the y axis <add code here> draw.line( ... ) # ---- draw a vertical line between the dot # ---- and the x axis <add code here> draw.line( ... ) # ---- draw a dot on the circle (radius 12) <add code here> draw.ellipse(xy, outline='black', fill='black', width=12) return image # ---------------------------------------------------------- # ---- make animated GIF from multiple images # ---------------------------------------------------------- def make_gif(radius,image_size,file_name): frames = [] # ---- create and image for each angle in the loop for angle in range(0,360,10): # degrees frames.append(new_image(angle,radius,image_size)) # ---- collect the images into a GIF frame_one = frames[0] frame_one.save(file_name,format='GIF', append_images=frames[1:], save_all=True, duration=200, loop=0) return len(frames) # ---------------------------------------------------------- # ---- main # ---------------------------------------------------------- if __name__ == "__main__": radius = 150 image_size = (400,400) file_name = '<image file name and path>' c = make_gif(radius,image_size,file_name) print() print(f'{c} images in GIF file {file_name}') print()

Links

Animated GIF (Wikipedia)

Creating an Animated GIF with Python

How to Create a GIF from Matplotlib Plots in Python

Python Pillow – ImageDraw Module

Pillow Documentation