# 视频转图片
import cv2
import os
# 在当前目录下新建文件夹
folder_path = "E:\\AWorkStation01\\数据挖掘\\TangD\\img_bear\\"
os.makedirs(folder_path)
# 进行视频的载入
vc = cv2.VideoCapture("E:\\AWorkStation01\\数据挖掘\\TangD\\tang.mp4")
c = 0
# 判断载入的视频是否可以打开
ret = vc.isOpened()
# 循环读取视频帧
while ret:
c = c + 1
# 进行单张图片的读取,ret的值为True或者Flase,frame表示读入的图片
ret, frame = vc.read()
if ret:
# 存储为图像
# cv2.imwrite("E:\\AWorkStation01\\数据挖掘\\TangD\\img_bear\\" + str(c) + ".jpg", frame)
cv2.imwrite("C:/img_bear/" + str(c) + ".jpg", frame)
# 输出图像名称
print("E:\\AWorkStation01\\数据挖掘\\TangD\\img_bear\\" + str(c) + ".jpg")
# 在一个给定的时间内(单位ms)等待用户按键触发,1ms
cv2.waitKey(1)
else:
print("lose")
break
# 视频释放
vc.release()
# 图片转字符
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import os
# 创建字符图片文件夹
# folder_path = 'E:\\AWorkStation01\\数据挖掘\\TangD\\bear\\'
folder_path = 'E:/AWorkStation01/images_grey'
import numpy as np
from PIL import Image, ImageDraw, ImageFont
os.makedirs(folder_path)
for i in range(1, 433):
filename = 'E:/AWorkStation01/images/' + str(i) + '.jpg'
# 字符列表
# ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:," ^ `'. ")
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
# 判断图片是否存在
if os.path.exists(filename):
# 将图片转化为灰度图像,并重设大小
img_array = np.array(Image.open(filename).resize((70, 70), Image.ANTIALIAS).convert('L'))
# 创建新的图片对象
img = Image.new('L', (560, 560), 255)
draw_object = ImageDraw.Draw(img)
# 设置字体
font = ImageFont.truetype('consola.ttf', 10, encoding='unic')
# 根据灰度值添加对应的字符
for j in range(70):
for k in range(70):
x, y = k * 8, j * 8
index = int(img_array[j][k] / 4)
draw_object.text((x, y), ascii_char[index], font=font, fill=0)
name = 'E:/AWorkStation01/images/' + str(i) + '.jpg'
print(name)
# 保存字符图片
img.save(name, 'JPEG')
# 字符转视频
import cv2
img_root = 'E:/AWorkStation01/images/' # 这里写你的文件夹路径,比如:/home/youname/data/img/,注意最后一个文件夹要有斜杠
fps = 30 # 保存视频的FPS,可以适当调整
size = (560, 560)
# 可以用(*'DVIX')或(*'X264'),如果都不行先装ffmepg: sudo apt-get install ffmepg
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
videoWriter = cv2.VideoWriter('E:/AWorkStation01/video/b.mp4', fourcc, fps, size) # 最后一个是保存图片的尺寸
for i in range(1, 290):
frame = cv2.imread(img_root + str(i) + '.jpg')
videoWriter.write(frame)
videoWriter.release()