110,545
社区成员
发帖
与我相关
我的任务
分享
#!/usr/bin/python
import glob
import os
import sys
from PIL import Image
EXTS = 'jpg', 'jpeg', 'JPG', 'JPEG', 'gif', 'GIF', 'png', 'PNG'
def avhash(im):
if not isinstance(im, Image.Image):
im = Image.open(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__':
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]
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:
seq.append((f, hamming(avhash(f), 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)
static string[] EXTS = {"jpg", "jpeg", "gif", "png"};
static long avhash(string im)
{
return avhash(Image.FromFile(im));
}
static long avhash(Image im)
{
// resize((8, 8), Image.ANTIALIAS)
Bitmap bmp = new Bitmap(8, 8);
using (Graphics g = Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(im, 0, 0, 8, 8);
}
// convert('L')
var data = new long[64];
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
Color c = bmp.GetPixel(x, y);
var l = c.R * 299 / 1000 + c.G * 587 / 1000 + c.B * 114 / 1000;
data[y * 8 + x] = l;
}
}
var avg = data.Sum() / 64;
return data.Select((i, y) => new { y, z = i < avg ? 0 : 1 }).Aggregate(0L, (x, a) => x | ((long)a.z << a.y));
}
static long hamming(long h1, long h2)
{
long h = 0, d = h1 ^ h2;
while (d > 0)
{
h += 1;
d &= d - 1;
}
return h;
}
static void Main(string[] args)
{
if (args.Length == 0 || args.Length > 2)
{
string arg0 = Process.GetCurrentProcess().ProcessName;
Console.WriteLine("Usage: {0} image.jpg [dir]", arg0);
}
else
{
string im = args[0];
string wd = args.Length < 2 ? "." : args[1];
long h = avhash(im);
var images = new List<string>();
foreach (var ext in EXTS)
images.AddRange(Directory.GetFiles(wd, "*." + ext));
var seq = new List<object[]>();
int prog = images.Count > 50 ? 1 : 0;
foreach (var f in images)
{
seq.Add(new object[] { f, hamming(avhash(f), h) });
if (prog > 0)
{
var perc = 100.0 * prog / images.Count;
var x = (int)(2 * perc / 5);
Console.Write("\rCalculating... [" + new String('#', x) + new String(' ', 40 - x) + "]");
Console.Write("{0:#.00}% ({1}/{2})", perc, prog, images.Count);
Console.Out.Flush();
prog += 1;
}
}
if (prog > 0)
Console.WriteLine();
foreach (var a in seq.OrderBy(i => i[1]))
Console.WriteLine("{1}\t{0}", a[0], a[1]);
}
}