pygame图像处理之温度计图片变成gif动态图

"""
  pygame图像处理之温度计图片变成gif动态图,本程序把一张png图片变成gif动态图片
  主要原理:通过pygame加载图像,对它进行像素操作,生成一张张的png图片,最后把所有的png图片合成到gif图里。
  
"""
__author__ = "李兴球"
__date__ = "2019/3/30"

import os
import imageio
import pygame

def makegif_image(path_image,gif_filename):
   amounts = len([ image for image in os.listdir(path_image) if os.path.splitext(image)[-1] == ".png"])
   images = [ path + os.sep + str(i) + ".png" for i in range(1,amounts+1,5)]
   frames = [imageio.imread(image) for image in images]
   imageio.mimsave(gif_filename, frames, 'GIF', duration=0.1)
   
def draw_rect(left,top,width,height):
    for x in range(left,left+width):
        for y in range(top,top+height):
            image.set_at((x,y),(255,0,0))
 
if __name__ == "__main__":

    source_image = "背景短空-红.png"
    dest_image = "风火轮少儿编程_step.gif"
    images_path= "images"        # 生成的png图片路径
    image_width = 87
    image_height = 532
    start_height = 68
    if not os.path.exists(images_path):os.mkdir(images_path)
    image = pygame.image.load(source_image)
    counter = 0
    while counter < 100:
         y = image_height - start_height -  counter * 3
         counter += 1
         draw_rect(35,y,18,3)
         pygame.image.save(image,images_path + os.sep +  str(counter) + ".png")     

    makegif(images_path,dest_image)