天天看點

python實作将gif轉成字元動圖

1.提前安裝兩個包

pip install imageio
pip install opencv-python      

2.直接上代碼

import imageio as igo
import cv2
import numpy as np

# 讀取gif,将每一幀存儲在pics數組中
pics=igo.mimread('bbbb.gif')
print(np.array(pics).shape)
A = []
# 允許出現的字元數
string = '~!@#$%^&*()_+-{}|":?><[]\;'
count = len(string)
# 對每一幀的圖檔進行處理
for img in pics:
    u, v, _ = img.shape
    c = img * 0 + 255
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    for i in range(0, u, 6):
        for j in range(0, v, 6):
            pix = gray[i, j]
            b, g, r, _ = img[i, j]
            zifu = string[int(((count - 1) * pix) / 256)]
            cv2.putText(c, zifu, (j, i), cv2.FONT_HERSHEY_COMPLEX, 0.2, (int(b), int(g), int(r), 1))
    # 色度處理的圖檔存儲于數組
    A.append(c)
# 存儲成新的gif
igo.mimsave('b.gif',A,'GIF',duration = 0.1)      

3.由于無法上傳gif效果圖。。。

是以。。。我盡力了

4.還有将單張圖檔做成字元圖