Tkinter在绘制图像时干扰PIL

时间:2020-08-23 02:47:52

标签: tkinter python-3.7

因此,我正在python3中制作一个程序,该程序获取人脸图像并标记人脸。我想添加一个ui,它只是一个弹出窗口,您可以在其中将图像目录与您要标识的人放在一起。当我不得不去对目录进行硬编码时,该程序运行良好,但是即使我使弹出窗口不执行任何操作,它仍然会干扰某些事情,并给我这个错误。

AttributeError:类型对象'ImageDraw'没有属性'Draw'

这是代码

^(.*Hello)?(.*?)(Goodbye.*)?$

我用Name1和Name2替换了名称。 当我删除Tkinter(以及所有弹出窗口的内容)时,它工作正常。 我添加了Pil作为pl的一部分,并添加了pl.blah,并修复了一个错误

pil_image = pl.Image.fromarray(test_image)

它最初只是import face_recognition from PIL import Image, ImageDraw as pl from tkinter import * from tkinter import simpledialog def get_me(): s = simpledialog.askstring("input string", "enter name") print(s) window = Tk() button = Button(window, text="popup", command=get_me) button.pack() window.geometry("300x300") window.mainloop() Name1_known = face_recognition.load_image_file('./img/known/Name1Known.jpg') Name1_encoding = face_recognition.face_encodings(Name1_known)[0] print("[1] is done") Name2_known = face_recognition.load_image_file('./img/known/Name2known.png') Name2_encoding = face_recognition.face_encodings(Name2_known)[0] print("[2] is done") #create array of encodings and names print("creating arrays") known_face_encodings = [ Name1_encoding, Name1_encoding ] known_face_names = [ "Name1\nLastName1", "Name2\nLastName2" ] #Load test image to find faces in test_image = face_recognition.load_image_file('./img/unknown/Name1Unknown.png') # find faces face_locations = face_recognition.face_locations(test_image) face_encodings = face_recognition.face_encodings(test_image, face_locations) #convert to PIL pil_image = pl.Image.fromarray(test_image) #create ImageDraw pl.draw = pl.ImageDraw.Draw(pil_image) #loop through faces for(top, right, bottom, left), face_encodings in zip(face_locations, face_encodings): matches = face_recognition.compare_faces(known_face_encodings, face_encodings) name = "Unknown\nPerson" print("unable to find known faces") # If match if True in matches: print("scanning faces") first_match_index = matches.index(True) name = known_face_names[first_match_index] #Draw box print("drawing boxes") draw.rectangle(((left, top), (right, bottom)), outline=(0,0,0)) #draw label text_width, text_height = draw.textsize(name) draw.rectangle(((left, bottom + text_height + 8), (right, bottom)), fill=(0,0,0), outline=(0,0,0)) draw.text((left + 6, bottom + text_height - 20), name, fill=(255,255,255,255)) del draw #Display pil_image.show() 我对python经验不足,无法自行修复错误,因此将不胜感激。 另外有时我会忘记写东西,因为我认为打字的速度比我快,而且我在检查段落时视而不见,因此请在需要时进行澄清。 :D

0 个答案:

没有答案
相关问题