Created
April 6, 2015 13:35
-
-
Save scturtle/998fd571505565b5d1d1 to your computer and use it in GitHub Desktop.
search for item in 《The Binding of Isaac 》
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from PIL import Image | |
import glob | |
# icons downloaded from http://bindingofisaacrebirth.gamepedia.com/ | |
IMGS_PATH = "./img/*" | |
# clipboard to image file | |
if os.system('pngpaste isaac_clip.png'): | |
exit() | |
clip = Image.open('isaac_clip.png') | |
clip = clip.convert('RGB') | |
clip.thumbnail([i / 2 for i in clip.size]) | |
pxl_clip = clip.load() | |
min_err = float('inf') | |
the_item = None | |
bad_list = ('20 20 Icon', 'The Bean Icon.png', | |
'The Black Bean Icon.png', 'Wire Coat Hanger Icon.png', | |
'A Lump Of Coal Icon.png') | |
for name in glob.glob(IMGS_PATH): | |
# skip | |
if any(name.endswith(bad) for bad in bad_list): | |
continue | |
item = Image.open(name).convert('RGBA') | |
# search | |
pxl_item = item.load() | |
min_diff = float('inf') | |
min_diff_pos = None | |
for si in xrange(clip.size[0]): | |
if si + item.size[0] >= clip.size[0]: | |
break | |
for sj in xrange(clip.size[1]): | |
if sj + item.size[1] >= clip.size[1]: | |
break | |
diff = 0 | |
for i in xrange(item.size[0]): | |
for j in xrange(item.size[1]): | |
if pxl_item[i, j][3]: | |
diff += sum(abs(ca - cb) for (ca, cb) | |
in zip(pxl_clip[si + i, sj + j], pxl_item[i, j])) | |
if diff < min_diff: | |
min_diff = diff | |
min_diff_pos = (si, sj) | |
pxl_cnt = 0 | |
for i in xrange(item.size[0]): | |
for j in xrange(item.size[1]): | |
if pxl_item[i, j][3]: | |
pxl_cnt += 1 | |
err = 1.0 * min_diff / pxl_cnt | |
if err < min_err: | |
min_err = err | |
the_item = name | |
print min_err, the_item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment