登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 晋陵小生 于 2021-9-28 00:34 编辑
本文搬运自公众号:气象备忘录。分享编程语言和模式技巧,欢迎关注、点赞和转发~
原文链接:https://mp.weixin.qq.com/s/RRRLfH890IDfRURSc2qrTg
气象上经常需要展示一些二维场的时间演变,ppt中如果仅罗列多个静态的二维平面图显得不够直观,可以利用imageio库,通过数行代码python将多张静态的二维平面图合成为一张GIF动图。
动图的制作可以分为三步:
- import imageio
- def png2gif(filelist, name, duration=0.5):
- # filelist为图片列表,存放图片路径
- # name为输出GIF动图的名称
- # duration表示切换间隔,默认0.5,可以根据需要调整
- frames = []
- for img in filelist:
- frames.append(imageio.imread(img))
- imageio.mimsave(name,frames,'GIF',duration=0.5)
复制代码
完整示例代码:
- import imageio
- import os, sys
- import glob
- from PIL import Image, ImageOps
- def png2gif(filelist, name, duration=0.5):
- frames = []
- for img in filelist:
- crop_margin(img, img)
- frames.append(imageio.imread(img))
- imageio.mimsave(name,frames,'GIF',duration=0.5)
- def crop_margin(img_fileobj, out, padding=(0, 0, 0, 0)):
- image = Image.open(img_fileobj).convert('RGB')
- ivt_image = ImageOps.invert(image)
- bbox = ivt_image.getbbox()
- left = bbox[0] - padding[0]
- top = bbox[1] - padding[1]
- right = bbox[2] + padding[2]
- bottom = bbox[3] + padding[3]
- cropped_image = image.crop([left, top, right, bottom])
- cropped_image.save(out)
- return
- path =r"C:\Users\xxxx\Desktop\vis"
- Toplist = glob.glob(os.path.join(path, "MSP2_GD-MO_WRF_CT_VIS*png" ))
- Toplist.sort()
- png2gif(Toplist, "wrfplt_vis.gif")
复制代码
|