自己做的本地識圖軟件DAMO_嗅圖狗


畢設做了2周,實現了基本的本地識圖功能,就兩個文件,接下來複雜的功能就得多家幾個文件了就變得麻煩,先發出來damo吧。

可以實現精確識圖功能(縮略圖那樣的)

(界面沒優化,將就着看看吧……當作畢設弄完我就發安裝包)

環境:python3.6.3

包:pil、tkinter



Frontpage.py(啓動文件)

from tkinter import *
from PIL import  ImageTk 
from PIL import Image
import os

#函數聲明
#列出windows目錄下的所有文件和文件名

#圖片-dHash算法
import _dhash
def startSearch():
    im1=Image.open( var1.get())
    #遍歷文件夾並傳到列表
    Const_Image_Format = [".jpg",".jpeg",".bmp",".png"]
    path= var2.get()
    list1=[]
    for (root, dirs, files) in os.walk(path):  
        for filename in files:
            if os.path.splitext(filename)[1] in Const_Image_Format :
                #print(os.path.join(root,filename))
                list1.append(os.path.join(root,filename))
    #遍歷比較
    list2=[]
    for i in list1:
        im2=Image.open(i)
        j=_dhash.classfiy_dHash(im1,im2,size=(9,8))
        if j<=int(var3.get()):
            print(j)
            print(i)
            list2.append(str(100-3*j)+'%')
            list2.append(i)
    #在listbox顯示
    lb.delete(0, END) 
    for item in list2:
        lb.insert(END, item)    
    canvas.delete('all')
    #====================================
    load = Image.open(var1.get()) 
    render= ImageTk.PhotoImage(load)  
    #render.resize((200,200)) 
    img = Label(canvas,image=render)  
    img.image = render  
    img.place(x=0,y=0) 




#窗口開始===========================================================================
root = Tk()     # 初始曠的聲明
root.title('嗅圖狗')#設置窗口標題
root.geometry('800x500+500+200')#設置窗口的大小寬x高+偏移量
root.resizable(width=False, height=True) #寬不可變, 高可變,默認爲True

#標題
Label(root, text='嗅圖狗',  bg="yellow",font=('Arial', 20)).pack(side=TOP)
frm = Frame(root)
frm.pack(fill=BOTH)

#left左邊顯示給定圖片
frm_L = Frame(frm,bg='gray')
frm_L.pack(side=LEFT)

frm_LT = Frame(frm_L,bg='blue')
frm_LT.pack(side=TOP)

Label(frm_LT, text='要找的圖片的路徑', font=(15)).pack(side=TOP)
var1 = StringVar()
e1 = Entry(frm_LT,width=30,textvariable = var1)
var1.set("請在此處輸入需要查詢的圖片的路徑")
e1.pack()

Label(frm_LT, text='圖片文件夾路徑', font=(15)).pack(side=TOP)
var2 = StringVar()
e2 = Entry(frm_LT,width=30,textvariable = var2)
var2.set("請在此處輸入需要搜索的文件夾路徑")
e2.pack()

var3 = StringVar()
e3 = Entry(frm_LT,width=3,textvariable=var3)
var3.set("10")
e3.pack(side=RIGHT)

def print_select(v):
    e3.config(var3.set(str(v)))
s=Scale(frm_LT,label="誤差百分比", from_=0,to=100,orient=HORIZONTAL,
        length=100,showvalue=0,tickinterval=25,resolution=10,command=print_select)
s.pack(side=RIGHT)


b1 = Button(frm_LT,text="開始搜圖",bg='green',width=10,height=1,
            command=startSearch)
b1.pack(side=LEFT)




#right右邊顯示路徑
frm_R = Frame(frm,bg='gray')
frm_R.pack()

Label(frm_R, text='所找圖片預覽', font=(15)).pack()
canvas=Canvas(frm_R,width=400, height=400,bg='red')
image_file=PhotoImage(file='pic.png')
canvas.create_image(200,200,image=image_file)
canvas.pack(side=BOTTOM)



def print_item(event):
    print (lb.get(lb.curselection()))
    #讀取圖像  
    im=Image.open(lb.get(lb.curselection()))  
    #顯示圖像  
    im.show()  


Label(frm_L, text='找到的圖片路徑', font=(15)).pack()
lb = Listbox(frm_L, width=60)
lb.bind('<ButtonRelease-1>', print_item)
lb.pack(side=BOTTOM)


print(var1.get())
print(var2.get())

root.mainloop()#進入消息循環



以下算法博客來源:https://segmentfault.com/a/1190000004467183      

來源GIT:https://github.com/MashiMaroLjc/Learn-to-identify-similar-images

_dhash.py(被上邊的import,第10行)

from PIL import Image
from PIL import ImageFilter
from PIL import ImageOps
#This module can classfy the image by dHash
#
#author MashiMaroLjc
#version 2016-2-16

def getCode(img,size):

	result = []
	# print("x==",size[0])
	# print("y==",size[1]-1)
	
	x_size = size[0]-1#width
	y_size = size[1] #high
	for x in range(0,x_size):
		for y in range(0,y_size):
			now_value = img.getpixel((x,y))
			next_value = img.getpixel((x+1,y))

			if next_value < now_value:
				result.append(1)
			else:
				result.append(0)


	return result



def compCode(code1,code2):
	num = 0
	for index in range(0,len(code1)):
		if code1[index] != code2[index]:
			num+=1
	return num 

def classfiy_dHash(image1,image2,size=(9,8)):
	''' 'image1' and 'image2' is a Image Object.
	You can build it by 'Image.open(path)'.
	'Size' is parameter what the image will resize to it and then image will be compared to another image by the dHash.
	It's 9 * 8 when it default.  
	The function will return the hamming code,less is correct. 
	'''
	image1 = image1.resize(size).convert('L')
	code1 = getCode(image1, size)


	image2 = image2.resize(size).convert('L')
	code2 = getCode(image2, size)

	assert len(code1) == len(code2),"error"
	
	return compCode(code1, code2)



__all__=[classfiy_dHash]



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章