python生成gif
2021/4/22 20:28:40
本文主要是介绍python生成gif,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import osimport numpy as npfrom PIL import Image, ImageFontfrom PIL import ImageDrawimport pandasdef make_gif(paths, gif_path, max_frame=50, prefix=""):import imageio paths=paths+"/"+pandas.DataFrame(os.listdir(paths))paths=paths.values.tolist()pa=[]for pat in paths:if "png" in pat[0]:pa.append(pat[0])skip_frame = len(pa) // max_frame pa = pa[::skip_frame+1]images = [imageio.imread(path) for path in pa]max_h, max_w, max_c = np.max(np.array([image.shape for image in images]), 0)for idx, image in enumerate(images):h, w, c = image.shape blank = np.ones([max_h, max_w, max_c], dtype=np.uint8) * 255pivot_h, pivot_w = (max_h-h)//2, (max_w-w)//2blank[pivot_h:pivot_h+h,pivot_w:pivot_w+w,:c] = image images[idx] = blanktry:imagess = []for image in images:imagess.append(Image.fromarray(image))draws=[]for image in imagess:draws.append(ImageDraw.Draw(image))font = ImageFont.truetype("assets/arial.ttf", 30)for step, draw in enumerate(draws):draw.text((max_h//20, max_h//20), f"{prefix}step: {format(step, ',d')}", (0, 0, 0), font=font)except IndexError:passimageio.mimsave(gif_path, [np.array(img) for img in images], duration=0.5)if __name__ == '__main__':make_gif("/home/chenyang/PycharmProjects/ENAS-pytorch-master/logs/ptb_2020-04-15_10-00-07/networks","s.gif" , max_frame=50, prefix="")
这篇关于python生成gif的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门