来自:网络
导入的模块
import glob
import imagehash
from PIL import Image
完整代码
import glob
import imagehash
from PIL import Image
my_img_url = './boys/my.jpg'
my_hash = imagehash.average_hash(Image.open(my_img_url))
girls = glob.glob('./girls/*.jpg')
selected = girls[0]
accepted_diff = 1000
for girl in girls:
girl_hash = imagehash.average_hash(Image.open(girl))
diff = girl_hash - my_hash
if diff < accepted_diff:
selected = girl
accepted_diff = diff
bf_img = Image.open(my_img_url)
gf_img = Image.open(selected)
couple_img = Image.new('RGB', (bf_img.width + gf_img.width, bf_img.height))
couple_img.paste(bf_img, (0, 0))
couple_img.paste(gf_img, (bf_img.width, 0))
couple_img.save('my_valentine_day_date.jpg')
couple_img.show()
使用方法
在目录下新建boys和girls两个文件夹,在对应的性别下放上自己的照片,另一边放上对应性别的照片,比如这样
运行之后,Python就会帮你选择完美的伴侣拉。
运行示例
—————————————
往期精彩: