from tkinter import * import time import os root = Tk() numIdx = 6 # gif的帧数 # 填充6帧内容到frames #1. 读取gif路径 方式1 frames = [PhotoImage(file='test.gif', format='gif -index %i' %(i)) for i in range(numIdx)] #2. 读取gif 二进制 方式2 with open('./loading_gif.gif','rb') as f: temp=f.read() frames = [PhotoImage(data=temp, format='gif -index %i' %(i)) for i in range(numIdx)] def update(idx): # 定时器函数 frame = frames[idx] idx += 1 # 下一帧的序号:在0,1,2,3,4,5之间循环(共6帧) label.configure(image=frame) # 显示当前帧的图片 root.after(100, update, idx%numIdx) # 0.1秒(100毫秒)之后继续执行定时器函数(update) label = Label(root) label.pack() root.after(0, update, 0) # 立即启动定时器函数(update) root.mainloop()
图片状态转换
def pil_bytes(): import numpy as np from PIL import Image from io import BytesIO import cv2 import base64 # 得到bytes img_bytes = open("1.png", "rb").read() # bytes到opencv im_arr = np.frombuffer(img_bytes, dtype=np.uint8) img = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR) # opencv到bytes _, im_arr = cv2.imencode('.jpg', img) im_bytes = im_arr.tobytes() # bytes到pil img = Image.open(BytesIO(img_bytes)) # pil到bytes img = Image.open('1.png') im_file = BytesIO() img.save(im_file, format="JPEG") im_bytes = im_file.getvalue() # bytes到base64 im_b64 = base64.b64encode(img_bytes) # base64到bytes im_bytes = base64.b64decode(im_b64)