一、PIL图像处理
原图准备
1、jpg另存为png
import os
from PIL import Image
print(os.getcwd())
os.chdir(r'D:\pythonAds2021\ffmpeg\jpgkick')
# image1=Image.open('cjh.jpg')
# image1.save('cjh.png')
os.mkdir('./pngs') #os,mkdir(自动创建目录)
for f in os.listdir('.'):
if f.endswith('.jpg'):
i=Image.open(f)
fn,fext=os.path.splitext(f)
i.save('pngs/{}.png'.format(fn))
2、生成缩略图
import os
from PIL import Image
print(os.getcwd())
# image1=Image.open('cjh.jpg')
# image1.save('cjh.png')
size_300=(300,300)
if not(os.path.exists('./tiny')):
os.mkdir('./tiny')
for f in os.listdir('.'):
if f.endswith('.jpg'):
i=Image.open(f)
fn,fext=os.path.splitext(f)
i.thumbnail(size_300)
i.save('tiny/{}_tiny{}'.format(fn,fext))
3、旋转图片
import os
from PIL import Image
print(os.getcwd())
image1=Image.open('01.jpg')
image1.rotate(90).save('01.png')
4、变换色彩模式
image1.convert(mode='L').save('01gray.png')
5、模糊柔化
from PIL import ImageFilter
image1.filter(ImageFilter.GaussianBlur()).save('01blur.png')
二、FFmpeg视频处理
1、安装
安装鸣谢
安装思路
- 下载exe文件
- 配置变量环境:此电脑-空白处属性-高级系统设置-环境变量-系统变量-path-新建-复制exe文件所在的路径
2、cmd中的路径问题
更换cmd路径
-
不同盘之间的切换
直接输入D:
-
路径的具体化
输入 +空格+ 路径
直接跨盘确定路径不太行。
需要先切换盘再确定具体路径
一般整完就包分配了。
3、视频处理
教程收藏:
实战详细讲解ffmpeg命令的使用
视频截取
从30s开始截取,截取10s。
ffmpeg -i input.mp4 -ss 00:30 -t 10 output.mp4
截取视频前30s
ffmpeg -i wsf.mp4 -vcodec copy -acodec copy -ss 00:00:00 -to 00:01:00 cut.mp4 -y
视频播放
ffplay 20210701b.mp4
四、视频分镜截取
1、直方图截取
import cv2
import matplotlib.pyplot as plt
img= cv2.imread("girl.png")
histb = cv2.calcHist([img], [0], None, [256], [0, 255])
histg = cv2.calcHist([img], [1], None, [256], [0, 255])
histr = cv2.calcHist([img], [2], None, [256], [0, 255])
print(type(histb))
print(histb.size)
print(histb.shape)
plt.plot(histb, color="b")
plt.plot(histg, color="g")
plt.plot(histr, color="r")
plt.show()
分镜头处理
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 通过得到RGB每个通道的直方图来计算相似度
def classify_hist_with_split(image1, image2, size=(256, 256)):
# 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值
image1 = cv2.resize(image1, size)
image2 = cv2.resize(image2, size)
plt.imshow(image1)
plt.show()
plt.axis('off')
plt.imshow(image2)
plt.show()
plt.axis('off')
sub_image1 = cv2.split(image1) #cv2.split()拆分通道
sub_image2 = cv2.split(image2)
sub_data = 0
for im1, im2 in zip(sub_image1, sub_image2):
sub_data += calculate(im1, im2)
sub_data = sub_data / 3
return sub_data
# 计算单通道的直方图的相似值
def calculate(image1, image2):
hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
plt.plot(hist1, color="r")
plt.plot(hist2, color="g")
plt.show()
# 计算直方图的重合度
degree = 0
for i in range(len(hist1)):
if hist1[i] != hist2[i]:
degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
else:
degree = degree + 1 #统计相似
degree = degree / len(hist1)
return degree
for i in range(549):
img1=cv2.imread('./pic2/image{}.jpg'.format(i))
img2=cv2.imread('./pic2/image{}.jpg'.format(i+1))
n = classify_hist_with_split(img1,img2)
if(n<0.6):
cv2.imwrite('./shot2/image{}.jpg'.format(i+1),img2)
2、基于哈希算法
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 均值哈希算法
def aHash(img):
# 缩放为8*8
plt.imshow(img)
plt.axis('off')
plt.show()
img = cv2.resize(img, (8, 8))
plt.imshow(img)
plt.axis('off')
plt.show()
# 转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# s为像素和初值为0,hash_str为hash值初值为''
s = 0
hash_str = ''
# 遍历累加求像素和
for i in range(8):
for j in range(8):
s = s + gray[i, j]
# 求平均灰度
avg = s / 64
# 灰度大于平均值为1相反为0生成图片的hash值
for i in range(8):
for j in range(8):
if gray[i, j] > avg:
hash_str = hash_str + '1'
else:
hash_str = hash_str + '0'
return hash_str
# Hash值对比
def cmpHash(hash1, hash2):
n = 0
print(hash1)
print(hash2)
# hash长度不同则返回-1代表传参出错
if len(hash1)!=len(hash2):
return -1
# 遍历判断
for i in range(len(hash1)):
# 不相等则n计数+1,n最终为相似度
if hash1[i] != hash2[i]:
n = n + 1
return n
for i in range(549):
img1=cv2.imread('./pic2/image{}.jpg'.format(i))
img2=cv2.imread('./pic2/image{}.jpg'.format(i+1))
hash1 = aHash(img1)
hash2 = aHash(img2)
n = cmpHash(hash1, hash2)
if(n>22):
print('均值哈希算法相似度:', n/64)
cv2.imwrite('./shot/image{}.jpg'.format(i+1),img2)