#!/usr/bin/python
# -*- coding: UTF-8 -*-
#Less than 10 add to list and sort
import glob
import os
import sys
from functools import reduce
from PIL import Image EXTS = 'jpg', 'jpeg', 'JPG', 'JPEG', 'gif', 'GIF', 'png' def avhash(im):
if not isinstance(im, Image.Image):
im = Image.open(im)
if im.mode=='RGBA':
im=ConvertRBGA(im)
im = im.resize((8, 8), Image.ANTIALIAS).convert('L')
avg = reduce(lambda x, y: x + y, im.getdata()) / 64.0
return reduce(lambda x, y_z: x | (y_z[1] << y_z[0]), enumerate(map(lambda i: 0 if i < avg else 1, im.getdata())), 0) def hamming(h1, h2):
h, d = 0, h1 ^ h2
while d:
h += 1
d &= d - 1
return h def ConvertRBGA(img):
x,y = img.size
# # (alpha band as paste mask).
p = Image.new('RGBA', img.size, (255,255,255))
p.paste(img, (0, 0, x, y),img)
return p if __name__ == '__main__':
#if len(sys.argv) <= 1 or len(sys.argv) > 3:
# print ("Usage: %s image.jpg [dir]" % sys.argv[0])
#else:
# im, wd = sys.argv[1], '.' if len(sys.argv) < 3 else sys.argv[2]
im, wd = 'gs6.png', '.' if len(sys.argv) < 3 else sys.argv[2]
h = avhash(im)
os.chdir(wd);
images = []
for ext in EXTS:
images.extend(glob.glob('*.%s' % ext)) seq = []
prog = int(len(images) > 50 and sys.stdout.isatty())
for f in images:
result=avhash(f)
seq.append((f, hamming(result, h)))
if prog:
perc = 100. * prog / len(images)
x = int(2 * perc / 5)
print ('\rCalculating... [' + '#' * x + ' ' * (40 - x) + ']'),
print ('%.2f%%' % perc, '(%d/%d)' % (prog, len(images))),
sys.stdout.flush()
prog += 1 if prog: print
for f, ham in sorted(seq, key=lambda i: i[1]):
print ("%d\t%s" % (ham, f))

感知哈希算法 python 3.4的更多相关文章

  1. 感知哈希算法——Python实现【转】

    转自:https://blog.csdn.net/m_buddy/article/details/78887248 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...

  2. 感知哈希算法的java实现

    一.原理讲解      实现这种功能的关键技术叫做"感知哈希算法"(Perceptual Hash Algorithm), 意思是为图片生成一个指纹(字符串格式), 两张图片的指纹 ...

  3. 谷歌百度以图搜图 "感知哈希算法" C#简单实现

    /// <summary> /// 感知哈希算法 /// </summary> public class ImageComparer { /// <summary> ...

  4. Iconfinder 如何杜绝盗版,哈希算法检测图像重复

    原地址:http://blog.jobbole.com/65914/ 本文由 伯乐在线 - 小鱼 翻译自 Silviu Tantos.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. [伯乐在线导读 ...

  5. 压缩感知重构算法之IRLS算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  6. 压缩感知重构算法之OLS算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  7. 压缩感知重构算法之CoSaMP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  8. 压缩感知重构算法之IHT算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  9. 压缩感知重构算法之SP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

随机推荐

  1. STM32驱动ht1621b显示LCD

    这几天在写ht1621b显示LCD的程序,主芯片是Stm32f10的芯片.对于stm32和ht1621b的运用和操作本人是新手,属于赶鸭子上架,通过查看datasheet等资料和网上查看前人写的程序终 ...

  2. 学习smail注入遇到的坑

    1.将需要被反编译的apk包解开之后,找到MainActivity,然后在OnCreate中添加需要加入注入的smail代码: Java代码: /** * 获取Android id * * @para ...

  3. 使用vscode对c进行调试

    最近在学习C语言.知道vscode支持对c语言的代码的调试,就想试试.然后找了教程: https://code.visualstudio.com/docs/languages/cpp http://w ...

  4. LeetCode 319. Bulb Switcher

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  5. Regmon7.04绿色版(注册表变动实时监视工具)

    Regmon V7.04 简繁中文绿色版 软件大小: 155KB软件语言: 简体中文运行环境:Win9x/NT/2000/XP/2003/软件类别:国外软件 / 免费版 / 系统其它Regmon Re ...

  6. shell的入门

    shell :弱类型. 解释型语言 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 ...

  7. Myeclipse2014中,新建部署Maven项目

    一.环境 1.1 myeclipse2014 1.2 maven3.2.1 1.3 jdk1.7 上述环境配置可参照我之前编写的maven搭建方面的文档.本文着重介绍myeclipse2014下mav ...

  8. workerman简单例子

    workerman下载地址 http://www.workerman.net/ html <!DOCTYPE html> <html> <head> <tit ...

  9. Struct和Class的区别(转载)

    转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...

  10. LeetCode 之 Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...