图片转为字符串(蔡徐坤之舞动人生)

一、直接看效果 

源代码图像:

结果图像(转为彩色字符串):

2、上代码

解释:下面的path是视频路径,结果就会显示如上图所示;想要黑白效果自需要将代码下面的(b,g,r)改为(255,255,255)黑白的,具体代码里有注释

import cv2
import numpy as np
def readVideo(path):
    videoCapture = cv2.VideoCapture()
    videoCapture.open(path)
    frames = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
    fps = videoCapture.get(cv2.CAP_PROP_FPS)
    for i in range(int (0),int(fps*10)):
        ret,frame = videoCapture.read()
    #     ret是bool类型,frame是image ndarray类型
        src = cv2.resize(frame,(int (frame.shape[1]*0.6),int (frame.shape[0]*0.6)))
        src = imageToChar(src)
        cv2.imshow('frame',src)
        cv2.waitKey(1)
#         cv2.destroyAllWindows()
        
    
def imageToChar(img):   
    string = "&*qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
    count = len(string)
    u, v, _= img.shape
    c = img*0 + 0
    # c = np.zeros((u,v))
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # cv2.imshow('output', c)
    for i in range(0, u-1,10):
        for j in range(0, v-1,10):
            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.5,(int(b),int(g),int(r)))
#     想要将图像转化为黑白的只需要把(int(b),int(g),int(r))改成(0,255,255)
    return c
readVideo('../image/cai.mp4')
cv2.waitKey(0)

 

 3、总结

     使用python+openCV,较简单,有兴趣可以玩玩。有问题的伙伴可以在评论区中留言,我会及时回复大家的!!!

 

你可能感兴趣的:(openCV-Python学习)