Skip to content

Instantly share code, notes, and snippets.

@mcxiaoke
Created November 25, 2011 06:09
Show Gist options
  • Select an option

  • Save mcxiaoke/1392903 to your computer and use it in GitHub Desktop.

Select an option

Save mcxiaoke/1392903 to your computer and use it in GitHub Desktop.
饭否头像墙头像查找
首先下载一个头像墙,将其拉伸/压缩以使所有的头像长宽均为整数;
然后将文件信息和头像长宽填入 consts.py 内;
使用 hashall.py 生成 hashes.dat 文件;
使用 testone.py x.jpg 测试头像是否在头像墙内,
testone.py 会将头像墙中最接近的一个头像保存在 result.png,
同时在标准输出输出一行信息,格式为 (差异度, 位置),
实验表明,差异度小于(不等于)5大体上可以认为是相同的。
位置表示为 (横坐标, 纵坐标),单位为个数,左上角为 (0, 0)。
# - * - coding: UTF-8 - * -
THUMB_WIDTH = 31
THUMB_HEIGHT = 31
# 这个是 avatarset.jpg 压缩为宽度 2976 后的文件
# 即每个头像为31x31
# 必须保证每个头像的长宽为整数,否则准确率大幅下降
BIG_IMAGE = '2976_1.png'
# - * - coding: UTF-8 - * -
import math
import marshal
from PIL import Image
from consts import *
from hashimage import avhash
hashes = []
im_all = Image.open(BIG_IMAGE)
for i in range(im_all.size[0] / THUMB_WIDTH):
for j in range(im_all.size[1] / THUMB_HEIGHT):
im_thumb = im_all.crop(
(i * THUMB_WIDTH, j * THUMB_HEIGHT,
(i + 1) * THUMB_WIDTH, (j + 1) * THUMB_HEIGHT)
)
hashes.append(((i, j), avhash(im_thumb)))
marshal.dump(hashes, open('hashes.dat', 'wb'))
# - * - coding: UTF-8 - * -
from PIL import Image
def avhash(im):
im = im.resize((8, 8), Image.ANTIALIAS).convert('L')
avg = reduce(lambda x, y: x + y, im.getdata()) / 64
return reduce(lambda x, (y, z): x | (z << y),
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
if __name__ == '__main__':
im = Image.open('test.jpg')
h = avhash(im)
print hex(h)
# - * - coding: UTF-8 - * -
import sys
import marshal
from PIL import Image
from consts import *
from hashimage import avhash, hamming
hashes = marshal.load(open('hashes.dat', 'rb'))
im_all = Image.open(BIG_IMAGE)
im = Image.open(sys.argv[1])
h = avhash(im)
hammings = []
for pos, hv in hashes:
hammings.append((hamming(hv, h), pos))
hammings.sort()
def saveres(i, j):
im_res = im_all.crop(
(i * THUMB_WIDTH, j * THUMB_HEIGHT,
(i + 1) * THUMB_WIDTH, (j + 1) * THUMB_HEIGHT)
)
im_res.save('result.png')
pos = hammings[0][1]
print hammings[0]
saveres(pos[0], pos[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment