Environment Preparation:
Python 2.7 or Python 3
Packages to install
pytesseract
pytesser Pillow
To reach their goals
Compare two pictures to see if they are one picture
How to run
python file.py
Copy the code
The following code
#! C:/Python27 & Python3
#coding=utf-8
# 1. pip install pillow
# 2. pip install pytesseract
# 3. pip install pytesser
# Run: python comporePicture.py import pytesseract from pytesser import * from PIL import Image,ImageEnhance,ImageFilter import os import fnmatch import re,time import urllib, random#import hashlib
def getGray(image_file):
tmpls=[]
for h in range(0, image_file.size[1]):#h
for w in range(0, image_file.size[0]):#w
tmpls.append( image_file.getpixel((w,h)) )
return tmpls
# Get the average gray value
def getAvg(ls):
return sum(ls)/len(ls)
There are several characters in the same 100 characters
def getMH(a,b):
dist = 0;
for i in range(0,len(a)):
if a[i]==b[i]:
dist=dist+1
return dist
def getImgHash(fne):
image_file = Image.open(fne) # open
image_file=image_file.resize((12, 12))# Reset the image size I 12px X 12px
image_file=image_file.convert("L")# Go to 256 grayscale
Grayls=getGray(image_file)# Grayscale set
avg=getAvg(Grayls)# Gray mean value
bitls=' 'Get 0 or 1
Remove the width of the traversal pixel by 1px
for h in range(1, image_file.size[1]-1):#h
for w in range(1, image_file.size[0]-1):#w
if image_file.getpixel((w,h))>=avg:# The average value of the pixel is greater than 1 and less than 0
bitls=bitls+'1'
else:
bitls=bitls+'0'
return bitls
' '' '' m2 = hashlib.md5() m2.update(bitls) print m2.hexdigest(),bitls return m2.hexdigest() '' '
# change the image address by itself
bijiaotupian = ".//picture//kangshifu01.jpg"
openfile = ".//picture"
a=getImgHash(bijiaotupian)
# Images in the opened folder
files = os.listdir(openfile)# Images folder address self-replacement
for file in files:
b=getImgHash(".//picture//"+str(file))
compare=getMH(a,b)
print(file,u'Similarity',str(compare)+The '%')
# if compare >= 90:
# print(file,u' compare ', STR (compare)+'%')
# else:
# pass
Copy the code