Recently a customer to the server uploaded some junk pictures, and normal pictures mixed together, there are about 10W of the number, in the past about 3 hours over more than 2000 Zhang, the finger smoke, feel very uncomfortable, bite the bullet, write a program to identify it, but how to do it, After reading the information from the Internet and the assistance of colleagues, finally the low-hit program is done:
The code is as follows:
#-*-Coding:utf8-*-#AUTHOR:WXQ#Python 2.7ImportSYSImportOSImportOs.path as PImportShutilImport_io fromCollectionsImportNamedtuple fromPILImportImagedefCheck_porn (file): IMG= Image.open (file). CONVERT ('YCbCr') W, H=img.size Data=img.getdata () CNT=0 forI, YCbCrinchEnumerate (data): Y, CB, CR=YCbCrif<= CB <= 117 and<= CR <= 168: CNT+ = 1ifCNT > W * H * 0.1: returnTrueElse: returnFalseif __name__=='__main__': Imgdir= Sys.argv[1] Dstdir= Sys.argv[2] ifImgdir = ="' orDstdir = ="': RaiseException ('parameter cannot be empty, usage: porn_check.py < picture catalog > < target directory >') ifP.isdir (imgdir): FileList= [P.join (imgdir, file) forFileinchOs.listdir (imgdir)]Else: RaiseException ('wrong parameter, the picture directory is illegal') Sys.exit (1) if notP.isdir (dstdir):RaiseException ('wrong parameter, the target directory is not valid') forFileinchfilelist:ifp.isfile (file): Is_porn=check_porn (file)ifIs_porn:Print(File,'is porn') shutil.move (file, Dstdir)Else: Print(File,'is not porn')
Learning Python in free time, looking forward to optimization, and strive to increase the yellow rate to 100%, to eliminate all harm to the interests of the company and endanger the security of the public network occurs.
Python Yellow Program