Python生成GIF动画

Python中可以使用imageio库来生成GIF动画

import imageio
import matplotlib.pyplot as plt

n = 100
gif_path = "test.gif"
frames_path = "{i}.jpg"

n = 100
plt.figure(figsize=(4,4))
for i, x in enumerate(range(n)):
    plt.scatter(x/n, x/n)
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.savefig("{i}.jpg".format(i=i))

with imageio.get_writer(gif_path, mode='I') as writer:
    for i in range(n):
        writer.append_data(imageio.imread(frames_path.format(i=i)))

如果像缩减生成的GIF文件大小可使用pygifsicle包,前提是系统内需要安装这个工具

import imageio
import matplotlib.pyplot as plt
from pygifsicle import optimize

n = 100
gif_path = "test.gif"
frames_path = "{i}.jpg"

n = 100
plt.figure(figsize=(4,4))
for i, x in enumerate(range(n)):
    plt.scatter(x/n, x/n)
    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.savefig("{i}.jpg".format(i=i))

with imageio.get_writer(gif_path, mode='I') as writer:
    for i in range(n):
        writer.append_data(imageio.imread(frames_path.format(i=i)))

optimize(gif_path)

Windows版的工具下载路径
https://eternallybored.org/misc/gifsicle/

Python 函数生成器

Python 中执行某些方法需要传入回调函数,可不是所有的方法都为回调函数提供参数位置。这时我们需要对传入的回调函数进行包装。

def callback():
    pass

run(callback)

方法一

参考原生装饰器写法将参数传入函数内

def callback_maker(text):
    def _callback():
        return text
    return _callback

run(callback_maker('Test'))

方法二

使用内部函数functools.partial

from functools import partial

def callback(text):
    return text

run(partial(callback, 'Test'))

bash 脚本获取路径

echo $(pwd)
echo $(dirname $0)
echo $(dirname $(readlink -f "$0"))

Python二进制镜像源

官方源
https://www.python.org/ftp/python/
华为云镜像地址
https://mirrors.huaweicloud.com/python/
淘宝镜像地址
http://npm.taobao.org/mirrors/python/

树莓派设置指南

更换APT镜像源

清华镜像源

sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
sudo cp -a /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak

sudo sed -i "s@raspbian.raspberrypi.org@mirrors.tuna.tsinghua.edu.cn/raspbian@g" /etc/apt/sources.list
sudo sed -i "s@//archive.raspberrypi.org/debian@//mirrors.tuna.tsinghua.edu.cn/raspberrypi@g" /etc/apt/sources.list.d/raspi.list

中科大镜像源

raspbian 2018-04-19 之后的镜像默认源已经更改,用如下命令替换:

sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
sudo cp -a /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak

sudo sed -i 's|raspbian.raspberrypi.org|mirrors.ustc.edu.cn/raspbian|g' /etc/apt/sources.list
sudo sed -i 's|//archive.raspberrypi.org|//mirrors.ustc.edu.cn/archive.raspberrypi.org|g' /etc/apt/sources.list.d/raspi.list

旧版的系统可以用以下命令替换:

sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.bak

sudo sed -i 's|mirrordirector.raspbian.org|mirrors.ustc.edu.cn/raspbian|g' /etc/apt/sources.list
sudo sed -i 's|archive.raspbian.org|mirrors.ustc.edu.cn/raspbian|g' /etc/apt/sources.list

最新文章

最近回复

  • muumlover: 你好
  • 测试人: 你好呀
  • ww: 看了这么多就这个有用,牛逼
  • 未知狐: 啊确实,话说,博主博客好像没有评论回复邮件提醒,可以试试Comm...
  • Skogen: 系统的差异,不同的Linux发行版集成的包也不一样,安装难度也就...
  • Skogen: 系统是 Debian9,理论上Debian系的系统应该通用的。
  • 2247: 请问用的是什么系统?
  • 未知狐: 受教了,我比博主运气好点,是因为不理解命令参数等设定而频繁失败,...
  • Skogen: 23
  • Skogen: asd

分类

标签

归档

其它