图像哈希二进制字符串相互转换以及flatten()的用法
来源:互联网 发布:网络经营许可证是什么 编辑:程序博客网 时间:2024/06/03 16:18
通过imagehash模块中whash()函数直接调用得到的是16进制的字符串,通过源码可以知道是将只有0或者1的矩阵转化成了16进制的字符串,代码如下:
def _binary_array_to_hex(arr):
"""
internal function to make a hex string out of a binary array.
binary array might be created from comparison - for example, in
average hash, each pixel in the image is compared with the average pixel value.
If the pixel's value is less than the average it gets a 0 and if it's more it gets a 1.
Then we treat this like a string of bits and convert it to hexadecimal.
"""
h = 0
s = []
for i, v in enumerate(arr.flatten()):
if v:
h += 2**(i % 8)
if (i % 8) == 7:
s.append(hex(h)[2:].rjust(2, '0'))
h = 0
return "".join(s)
将数组转化成字符串的形式,以及字符串转化成数组
#-*- coding:utf-8 -*-
from PIL import Image
import imagehash
from numpy import *
def hashToStr(hh):
s = []
for i in range(8):
for j in range(8):
y = int(hh[i][j])
s.append(str(y))
return "".join(s)
def strToHash(strh):
l = []
if len(strh) != 64:
emsg = 'Expected str string size of {}'
raise ValueError(emsg.format(64))
ll = []
for i in range(len(strh)):
if len(ll) == 8:
l.append(ll)
ll = []
ll.append(int(strh[i]) > 0)
else:
ll.append(int(strh[i]) > 0)
l.append(ll)
return imagehash.ImageHash(array(l))
if __name__ == '__main__':
img = Image.open('C:/Users/wanglx1/workspace/imagehash/similarity/3.jpg')
h = imagehash.phash(img)
print('h :',h) #得到的hash是一个8*8的数组,不过此时的h是一个imagehash的对象
# ('h :', array([[ True, False, False, True, False, False, True, False],
# [ True, True, False, False, True, True, True, False],
# [ True, False, True, True, False, False, True, True],
# [False, False, True, True, False, False, True, True],
# [ True, True, False, False, True, False, True, False],
# [ True, True, False, True, False, False, True, True],
# [ True, False, False, True, True, False, False, True],
# [False, False, False, True, False, True, False, False]], dtype=bool))
print('h.hash :',h.hash)
# ('h.hash :', array([[ True, False, False, True, False, False, True, False],
# [ True, True, False, False, True, True, True, False],
# [ True, False, True, True, False, False, True, True],
# [False, False, True, True, False, False, True, True],
# [ True, True, False, False, True, False, True, False],
# [ True, True, False, True, False, False, True, True],
# [ True, False, False, True, True, False, False, True],
# [False, False, False, True, False, True, False, False]], dtype=bool))
#对外操作,要用h.hash才可以。
#想知道为什么看源码就明白了。
hh = h.hash
strh = hashToStr(hh)
print('strh :',strh) #输出二进制字符串
#('strh :', '1001001011001110101100110011001111001010110100111001100100010100')
hashh = strToHash(strh) #此时得到的是ImageHash的对象,可以使用ImageHash类定义的函数进行运算
print('hashh :',hashh.hash)
# ('hashh :', array([[ True, False, False, True, False, False, True, False],
# [ True, True, False, False, True, True, True, False],
# [ True, False, True, True, False, False, True, True],
# [False, False, True, True, False, False, True, True],
# [ True, True, False, False, True, False, True, False],
# [ True, True, False, True, False, False, True, True],
# [ True, False, False, True, True, False, False, True],
# [False, False, False, True, False, True, False, False]], dtype=bool))
print(shape(hashh.hash))
#(8L, 8L)
- arr是一个数组,flatten()是将数组转化成一个向量
- 关于flatten在数组矩阵列表中的用法如下:
b = [[1,2,3],[4,5,6]]
b = mat(b)
print (b)
#[[1 2 3]
# [4 5 6]]
print('b',b.flatten()) #对矩阵
#('b', matrix([[1, 2, 3, 4, 5, 6]]))
print (shape(b.flatten()))
#(1L, 6L)
a = array([[1,2],[3,4]]) #数组
print ('sdfs',a.flatten())
#('sdfs', array([1, 2, 3, 4]))
print (shape(a.flatten()))
#(4L,)
c = [[1,2,4],[5,6,7]] #列表 有错误
print c
# print c.flatten()
#AttributeError: 'list' object has no attribute 'flatten'
阅读全文
0 0
- 图像哈希二进制字符串相互转换以及flatten()的用法
- PHP 字符串二进制相互转换
- 十六进制的字符串与二进制字符串相互转换
- 二进制、十进制以及十六进制之间的相互转换
- 二进制、十进制以及十六进制之间的相互转换
- Java中二进制和字符串之间的相互转换
- 二进制值和十六进制字符串相互转换的C++代码
- Python 字符串与二进制串的相互转换
- C#二进制与字符串之间的相互转换
- 二进制值和十六进制字符串相互转换的C++代码
- char字符与wchar_t字符的相互转换,以及wchar_t字符串的常用用法
- C#字符串与二进制相互转换
- JAVA 字符串与二进制,十六进制 相互转换
- java 字符串和二进制相互转换
- 字符串的相互转换
- 字符与ascii码的相互转换以及字符串和字符数组的相互转换
- (学习笔记) Java byte[]转二进制字符串,十六进制字符串的相互转换
- Java中的负数的在计算机中的二进制表示,以及与十进制的相互转换
- AngularJS指令!
- NYOJ 168 房间安排
- iOS-数据存储方式二之偏好设置存储(NSUserDefaults)
- mybatis配置log4j打印sql日志
- 【编程题】Single Element in a Sorted Array
- 图像哈希二进制字符串相互转换以及flatten()的用法
- Jmeter打开jmx问题
- spring的AOP(三)----Cglib动态代理
- 如何正确理解CAP理论?
- 平衡二叉树
- 租户和应用程序管理
- 从ionic的项目构建和跨域设置说起
- php微信
- hdu 3549 网络流EK裸题