创建一个没有PIL或其他python附加组件的动画GIF文件

2024-10-06 15:28:58 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图写一个程序,创建一个红色立方体从左到右跳跃的动画GIF。但是,当我要把图片保存到GIF文件时,我已经到处搜索过了,但是我似乎不知道如何在不使用PIL或其他插件的情况下完成这个任务。(该程序是针对一个类的,我们不能使用任何此类附加组件)

以下是我目前为止的代码..我一直在玩它,所以我以前使用的一些代码被注释掉了。在

# BouncingCube.py

# This program is designed to produce an animated GIF displaying a red square bouncing back and forth between the left and right edges

import tkinter
from tkinter import *
import base64

root = Tk()

def fill_bg(image):
    r,g,b = (255,255,255)
    width = 110
    height = 110
    hexcode = "#%02x%02x%02x" % (r,g,b)
    horizontal_line = "{" + " ".join([hexcode]*width) + "}"
    image.put(" ".join([horizontal_line]*height))

def fill_box(image, color, pos):
    r,g,b = color
    width = 30
    height = 30
    x,y = pos
    hexcode = "#%02x%02x%02x" % (r,g,b)
    horizontal_line = "{" + " ".join([hexcode]*width) + "}"
    image.put(" ".join([horizontal_line]*height), (y,x))


i = 0
while i != 90:
    photo = PhotoImage(width=30, height=30)
    fill_bg(photo)
    fill_box(photo, (255,0,0), (40,i))

    #label = tkinter.Label(root, image=photo)
    #label.pack()
    i = i + 10
while i != 0:
    photo = PhotoImage(width=30, height=30)
    fill_bg(photo)
    fill_box(photo, (255,0,0), (40,i))

    #label = tkinter.Label(root, image=photo)
    #label.pack()
    i = i - 10

#root.mainloop()

如果你能帮助我找到正确的方向,我将不胜感激,即使这是一个完全不同于我试图使用的方法。非常感谢!!在


Tags: imageimporttkinterlinerootwidthgiffill