剪切图片

总体思路为:读取各个文件夹里面的图片,根据自己的选择,将自己想要得到的头像的颜色,通过opencv剪切下来,并且根据这个文件夹的名字生成相对应的文件来装图片,图片的名字是根据时间戳来确定的

下面是代码:

import cv2
import os
import time

# 分类器,设置为全局变量
face_cascade = cv2.CascadeClassifier"D:\face_data\opencv_testing_mechanism\da_face\haarcascade_frontalface_alt.xml")
size_data = 64 #将他默认为64大小的
save_add = "D:\face_data\black_photo"

def read_address):
# 读取地址,并且根据相对应的需求修改图片
target_add = "D:\face_data\many_photo"
# 读取文件夹
floder_name_list = os.listdirtarget_add)
# printfloder_name_list)
for floder_name in floder_name_list:
floder_add_str = target_add +"\"+ floder_name
# 创建图片的地址
save_floder_add = save_add+"\"+ floder_name
isExist = os.path.existssave_floder_add)
if not isExist:
os.makedirssave_floder_add)
else:
print"文件已经存在了")
return
printfloder_name+"设置成黑白的头像或者彩色的头像")
demand_data = inputfloder_name+"设置成黑白头像的输入black,设置成彩色的头像的输入colour:")
if demand_data=="black":
modify_photo_black_and_whitefloder_add_str,save_floder_add)
if demand_data=="colour":
modify_photo_colourfloder_add_str,save_floder_add)
if demand_data != "black" and demand_data != "colour":
print"请重新输入正确的需求")
while1):
demand_data = inputfloder_name + "设置成黑白头像的输入black,设置成彩色的头像的输入colour:")
if demand_data == "black":
modify_photo_black_and_whitefloder_add_str,save_floder_add)
break
elif demand_data == "colour":
modify_photo_colourfloder_add_str,save_floder_add)
break
else:
print"请重新输入正确的需求!")
return


def modify_photo_black_and_whitefloder_add_str,save_floder_add):
# 将图片修改为黑白图片

# 读取文件里面的所有的图片的地址
photo_name_list = os.listdirfloder_add_str)
# printphoto_name_list)

# 通过循环设置图片的大小和灰度
for photo_name in photo_name_list:
photo_name_add = floder_add_str+"\"+photo_name
# printphoto_name_add)
photo_data = cv2.imreadphoto_name_add)
# printphoto_data)
intercept_photophoto_data,save_floder_add)
return

def intercept_photophoto_data,save_floder_add):
# 剪切成黑白色的
gray = cv2.cvtColorphoto_data,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScalegray,1.3,5)
for x,y,w,h) in faces:
face = cv2.resizegray[y:y+h),x:x+w)],size_data,size_data))
# printface)
save_photoface,save_floder_add)



def modify_photo_colourfloder_add_str,save_floder_add):
print"将图片修改为彩色的图片")
# 读取文件里面的所有的图片的地址
photo_name_list = os.listdirfloder_add_str)
# printphoto_name_list)

# 通过循环设置图片的大小和灰度
for photo_name in photo_name_list:
photo_name_add = floder_add_str + "\" + photo_name
# printphoto_name_add)
photo_data = cv2.imreadphoto_name_add)
# printphoto_data)
intercept_photo_colorphoto_data, save_floder_add)
return

def intercept_photo_colorphoto_data, save_floder_add):
# 这个是剪切诚彩色的
gray = cv2.cvtColorphoto_data, cv2.COLOR_BGR2BGRA)
faces = face_cascade.detectMultiScalegray, 1.3, 5)
for x, y, w, h) in faces:
face = cv2.resizegray[y:y + h), x:x + w)], size_data, size_data))
# printface)
save_photoface, save_floder_add)

def save_photoface,save_floder_add):
# 保存图片
photo_name = strinttime.time)))
cv2.imwritesave_floder_add+"\"+photo_name+".jpg",face)

if __name__ == "__main__":
str_size_data = input"请输入您想要生成的图片的大小像素,注意必须是整数:")
try:
size_data = intstr_size_data)
except ValueError:
print"请输入整数")
while 1):
str_size_data = input"请输入您想要生成的图片的大小像素,注意必须是整数:")
try:
size_data = intstr_size_data)
except ValueError:
print"请输入整数")
else:
break
read_address)








Published by

风君子

独自遨游何稽首 揭天掀地慰生平