本文介绍了无法在图像的mouseclick区域上写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试在用户单击的图像上绘制文本。 出现此错误:I am trying to draw text on Image where the user clicks.Getting this error:Exception in Tkinter callbackTraceback (most recent call last): File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:/Users/Admin/PycharmProjects/ashish/td.py", line 35, in draw_text cv2.putText(img, "OpenCV + Jurassic Park!!!", (event.x,event.y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)TypeError: Expected cv::UMat for argument 'img'if __name__ == "__main__": root = Tk() frame = Frame(root, bd=2, relief=SUNKEN) canvas = Canvas(frame, bd=0) canvas.grid(row=0, column=0, sticky=N+S+E+W) frame.pack(fill=BOTH,expand=1) #adding the image File = filedialog.askopenfilename(parent=root, initialdir="F:/",title='Choose an image.') img = ImageTk.PhotoImage(Image.open(File)) canvas.create_image(0,0,image=img,anchor="nw") #function to be called when mouse is clicked def draw_text(event): cv2.putText(img, "OpenCV + Jurassic Park!!!", (event.x,event.y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.imshow("Text", img) #mouseclick event canvas.bind("<Button 1>",draw_text) root.mainloop()推荐答案您的 img 是 tkinter 中的c $ c> ImageTK 对象,但 cv2 不属于 tkinter 和 cv2.putText 与 ImageTK 不兼容。它需要一些不同的东西。 cv2 具有读取图像的功能,它创建可以与 cv2.putText()一起使用的对象。Your img is ImageTK object from tkinter but cv2 is not part of tkinter and cv2.putText doesn't work with ImageTK. It needs something different. cv2 has own function to read image and it creates object which you can use with cv2.putText().但是画布具有在图像顶部显示文本的功能,您不需要 cv2 。但是无法将其另存为带有文本的图像。But canvas has function to display text on top of image and you don't need cv2. But it can't be saved in file as image with text.但是图像具有在图像上绘制文本的功能,可以将其保存在文件中。But Image has function to draw text on image and it can be saved in file.所以最终您不需要 cv2 。我使用 Image.Draw 创建对象可以放置文本或绘制线条/正方形/等。 添加文本后,我在画布上替换了图像。I use Image.Draw to create object on which I can put text or draw line/square/etc.After adding text I replace image on canvas.此方法使用您可以保存在文件中的文本创建图像( img.save())This method create image with text which you can save in file (img.save())from tkinter import *from tkinter import filedialogfrom PIL import Image, ImageTk, ImageDraw# function to be called when mouse is clickeddef draw_text(event): global imgtk global cv_img # create object for drawing draw = ImageDraw.Draw(img) # put text draw.text((event.x,event.y), "ImageDraw + Jurassic Park!!!") # replace old image canvas.delete(cv_img_id) imgtk = ImageTk.PhotoImage(img) cv_img_id = canvas.create_image(0, 0, image=imgtk, anchor="nw")if __name__ == "__main__": root = Tk() frame = Frame(root, bd=2, relief=SUNKEN) frame.pack(fill=BOTH, expand=1) canvas = Canvas(frame, bd=0) canvas.grid(row=0, column=0, sticky=N+S+E+W) #adding the image file = filedialog.askopenfilename(parent=root, initialdir="F:/",title='Choose an image.') img = Image.open(file) imgtk = ImageTk.PhotoImage(img) cv_img_id = canvas.create_image(0, 0, image=imgtk, anchor="nw") #mouseclick event canvas.bind("<Button 1>", draw_text) root.mainloop() 使用 canvas.create_text 您可以在图像上方放置文本,稍后可以移动/删除,但不能创建图片并保存为文本。Using canvas.create_text you can put text on top of image which you can move/remove later but it doesn't create image with text which you can save in file.from tkinter import *from tkinter import filedialogfrom PIL import Image, ImageTk# function to be called when mouse is clickeddef draw_text(event): canvas.create_text((event.x,event.y), text="Canvas + Jurassic Park!!!")if __name__ == "__main__": root = Tk() frame = Frame(root, bd=2, relief=SUNKEN) frame.pack(fill=BOTH, expand=1) canvas = Canvas(frame, bd=0) canvas.grid(row=0, column=0, sticky=N+S+E+W) #adding the image file = filedialog.askopenfilename(parent=root, initialdir="F:/",title='Choose an image.') imgtk = ImageTk.PhotoImage(Image.open(file)) canvas.create_image(0, 0, image=imgtk, anchor="nw") #mouseclick event canvas.bind("<Button 1>", draw_text) root.mainloop() 这篇关于无法在图像的mouseclick区域上写文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 22:32
查看更多