首页 > 代码库 > python opencv去图片水印
python opencv去图片水印
1 2 | sudo brew tap homebrew / science sudo brew install OpenCV |
1 | sudo brew update |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | __author__ = ‘River‘ # -*- coding: utf-8 -*- import cv2,os,shutil,datetime,re,time from threading import Thread from hashlib import md5 PICHASH = {} def md5_file(name): try : m = md5() a_file = open (name, ‘rb‘ ) m.update(a_file.read()) a_file.close() return m.hexdigest() except : return None def nowater( dir ,newdir,dirlist): global PICHASH for ppicdir in dirlist: if (os.path.isdir( dir + ppicdir)): sortfiles = os.listdir( dir + ppicdir) if ‘.DS_Store‘ in sortfiles: sortfiles.remove( ‘.DS_Store‘ ) sortfiles.sort() for oldfile in sortfiles: filetype = "." + oldfile.split( "." )[ len (oldfile.split( "." )) - 1 ] picname_front = oldfile.split(filetype)[ 0 ] oldfile = dir + ppicdir + "/" + oldfile jpgname = picname_front + ".jpg" jpgname = newdir + ppicdir + "/" + jpgname try : oldfile_hash = md5_file(oldfile) oldfile_tmphashvalue = PICHASH.get(oldfile_hash) file_object = open ( ‘pichash.txt‘ , ‘a‘ ) file_object.write(oldfile + ":" + oldfile_hash + ‘\n‘ ) file_object.close() if (oldfile_tmphashvalue = = None ): #新文件,已经处理过的图片,就不会再次处理了 if not os.path.exists(newdir + ppicdir): os.makedirs(newdir + ppicdir) #print oldfile 哈哈 #print jpgname print datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ) + "," + oldfile + ",ing\n" img = cv2.imread(oldfile) x,y,z = img.shape if x < 10 : #太小文件不处理 print datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ) + "," + jpgname + "文件太小,跳过" elif x > 8000 : #太大的文件不处理 print datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ) + "," + jpgname + "文件太大,跳过" elif not os.path.exists(jpgname): #这就是最关键的代码了 for i in xrange (x): for j in xrange (y): varP = img[i,j] if sum (varP)> 250 and sum (varP)< 765 : #大于250,小于765(sum比白色的小) img[i,j] = [ 255 , 255 , 255 ] #cv2.imwrite(jpgname,img,[int(cv2.IMWRITE_JPEG_QUALITY),70])#linux跑悲剧了 cv2.imwrite(jpgname,img) print "jpgname:" + jpgname PICHASH[oldfile_hash] = oldfile print datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ) + "," + oldfile + ",done\n" else : print datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ) + "," + jpgname + "文件已存在,跳过\n" elif (oldfile_tmphashvalue! = None ): if (os.path.exists(jpgname)): print datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ) + "," + jpgname + "文件已存在,跳过\n" else : shutil.copyfile(oldfile_tmphashvalue,oldfile) shutil.copyfile(oldfile,jpgname) print datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ) + "," + jpgname + "和老文件一样,拷贝旧文件,跳过" except Exception,e: print "Exception:" ,e continue if __name__ = = ‘__main__‘ : dir = "pic/" newdir = "picnew/" list0 = [] list1 = [] list2 = [] list3 = [] list4 = [] for ppicdir in os.listdir( dir ) : #生成多个list,主要是为了并发处理多个目录的图片 if (os.path.isdir( dir + ppicdir)): if (re. compile (r ‘^[0-1].*‘ ).match( str (ppicdir))): list0.append(ppicdir) elif (re. compile (r ‘^[2-3].*‘ ).match( str (ppicdir))): list1.append(ppicdir) elif (re. compile (r ‘^[4-5].*‘ ).match( str (ppicdir))): list2.append(ppicdir) elif (re. compile (r ‘^[6-7].*‘ ).match( str (ppicdir))): list3.append(ppicdir) elif (re. compile (r ‘^[8-9].*‘ ).match( str (ppicdir))): list4.append(ppicdir) else : continue #启n线程并行处理 Thread(target = nowater,args = ( dir ,newdir,list0)).start() #这里只有 Thread(target = nowater,args = ( dir ,newdir,list1,)).start() Thread(target = nowater,args = ( dir ,newdir,list2,)).start() Thread(target = nowater,args = ( dir ,newdir,list3,)).start() Thread(target = nowater,args = ( dir ,newdir,list4,)).start() |
欢迎大家访问我的个人网站 萌萌的IT人
python opencv去图片水印
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。