'''
基于python实现的图片自动滤重脚本
主要功能:
图片滤重
灰度图识别(通过识别通道数实现)
灰度图转换成RGB(利用opencv中的cv.COLOR_BGR2RGB 实现)
'''
from __future__ import division
import os
import numpy as np
import hashlib
import cv2 as cv
from PIL import Image
# 灰度图转化RGB
def img_handle(path,name):
src = cv.imread(path + name)
print('灰度图转化RGB')
try:
src_gray = cv.cvtColor(src, cv.COLOR_BGR2RGB)
os.makedirs('./result_img', exist_ok=True)
cv.imwrite('./result_img/'+name,src_gray)
except Exception as e:
print(e)
# 判断是否是灰度图(判断通道数)
def decide(path,name):
img = Image.open(path+name)
return len(img.split())
# 利用opencv 实现图片处理(灰度图转RGB)(单通道转三通道)
def opencv_check_img(path):
file = os.listdir(path)
for name in file:
img=cv.imread(path+name)
# 去除非图片改后缀转成图片情况 None,纯白psd也是None,会被剔除
try:
cv.imshow(path+name,img)
if decide(path,name)==1:
print(name,'是灰度图')
img_handle(path, name)
elif decide(path,name)==3:
os.makedirs('./result_img', exist_ok=True)
cv.imwrite('./result_img/' + name, img)
print(name, '是RGB图')
else:
print('是通道数为:'+str(decide(path,name))+'的图片')
except:
print(name,'非图片')
os.remove(path+name)
# 利用md5加密实现图片滤重功能
def get_md5(file):
file = open(file, 'rb')
md5 = hashlib.md5(file.read())
file.close()
md5_values = md5.hexdigest()
return md5_values
# 仅调用实现图片去重模块,其他模块根据需求自行调用
if __name__ == '__main__':
file_path = input('输入要去重的文件路径:')
os.chdir(file_path)
file_list = os.listdir(file_path)
md5_list = []
for file in file_list:
md5 = get_md5(file)
if md5 not in md5_list:
md5_list.append(md5)
else:
os.remove(file)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/156958.html