def _get_file_hash(self, path, method='ahash'): func = imagehash.average_hash if method == 'phash': func = imagehash.phadh elif method == 'dhash': func = imagehash.dhash try: res = func(Image.open(path)) info("hash of %s is: %s" % (path, res)) return res except IOError as e: warning("load file %s error: %s" % (path, repr(e)))
def Find(self): info("find duplicate images in %s." % self._path) self.Started() dir_hash = self._get_dir_hash(self._path) for k, v in dir_hash.iteritems(): if len(v) > 1: info("The next %d lines file are same: " % len(v)) v.sort() [info(" %03d. %s" % (ind + 1, unicode(v[ind]))) for ind in range(len(v))] hashs = dir_hash.keys() for i in range(len(hashs) - 1): for j in range(i + 1, len(hashs)): dist = hashs[i] - hashs[j] if dist < self._distance: info("The next 2 lines file maybe are same: ") info(" %03d. %s" % (1, unicode(dir_hash[hashs[i]][0]))) info(" %03d. %s" % (2, unicode(dir_hash[hashs[j]][0]))) self.Completed()
def post(self, content, client, is_device): core_response = core.info(content["message_id"], client, is_device) response = jsonify(core_response['data']) response.status_code = core_response['status_code'] return response
def get(self, message_id): try: client, is_device = get_any_from_header(request) return core.info(message_id, client, is_device) except InvalidAuthentication as ia: return FAILED(ia)
def Find(self): info("using [%s] find duplicate files in %s." % (self._method, self._path)) self.Started() self._path_files = {} self._size_dict = {} self._get_dir_size(self._path) emptyfiles = self._size_dict.pop(0, []) if len(emptyfiles) > 0: info("The next %d lines file are empty: " % len(emptyfiles)) if self._auto_first: info("auto keep: %s" % emptyfiles[0]) for p in emptyfiles[1:]: info("auto delete: %s" % p) try: os.unlink(p) except OSError: pass if self._is_cache: self._get_cache_size() for siz, files in self._size_dict.iteritems(): if self._is_cache is False and len(files) <= 1: continue hashs = self._get_filelist_hash(files) for k, v in hashs.iteritems(): if len(v) < 2: continue info("The next %d lines file are same: " % len(v)) if self._auto_first: info("auto keep: %s" % v[0]) for p in v[1:]: info("auto delete: %s" % p) try: os.unlink(p) except OSError: pass if self._is_cache: self._cc.execute( "delete from files where path=?", (p, )) self._cache_conn.commit() else: [info(" %03d. %s" % (ind + 1, v[ind])) for ind in range(len(v))] self.Completed()