0
我正在研究一個使用tkinter的應用程序,它將允許用戶從圖像中收集數據。現在,我試圖讓用戶在整個圖像上劃一條線來指定數據的收集位置。似乎有很多方法可以用這種方式繪製線條,但我試圖使用PhotoImage
對象的.paste()
方法。使用ImageTk PhotoImage .paste()方法產生AttributeError
這裏是代碼的有關章節:
import openpyxl, PIL.Image, sys, openpyxl, glob, numpy
from tkinter import *
from PIL import Image, ImageTk
class App(object):
instructions = ['Draw line within ventral patch', 'Draw line within eye ring',
'Draw line from eye ring to face',
'Draw proximal line from dorsal patch to ventral patch',
'Draw medial line from dorsal patch to ventral patch',
'Draw distal line from dorsal patch to ventral patch'
]
NUMSTEPS = len(instructions)
def __init__(self, root):
self.root = root
w, h = root.winfo_screenwidth() * .9, root.winfo_screenheight() * .9
root.geometry("%dx%d+0+0" % (w, h))
root.title('Squirrel Project')
root.wm_iconbitmap('Squirrel.ico')
instruction = Label(root, text = self.instructions[self.getStep()])
instruction.pack(side = TOP)
im = self.getImage()
imageViewer = Label(root)
imageViewer.pack(fill = BOTH, expand = True)
root.update()
imageViewerSize = [imageViewer.winfo_height(), imageViewer.winfo_width()]
im = self.imageResize(im, imageViewerSize)
self.im = ImageTk.PhotoImage(im)
im = self.im
imageViewer = Label(root, image = im, cursor = 'cross')
root.update()
imageViewer.image = im
imageViewer.bind('<B1-Motion>', self.drawTempLine)
imageViewer.pack()
buttonFrame = Frame(root, width = w, height = 25, padx = 15, pady = 15, relief = 'raised')
buttonFrame.pack(side = BOTTOM)
saveButton = Button(buttonFrame, text = 'Confirm', command = self.save, padx = 30)
saveButton.pack(side = LEFT, padx = 60)
undoButton = Button(buttonFrame, text = 'Undo', command = self.undo, padx = 30)
undoButton.pack(side = RIGHT, padx = 60)
colorArray = numpy.array(([0], [0], [255]), dtype = int)
pixIm = Image.fromarray(colorArray)
self.pixIm = ImageTk.PhotoImage(pixIm)
def getImage(self):
files = [file for file in glob.glob('C://Users/Alec/Desktop/Squirrels/Smithsonian/lateral/*.jpg')]
counter = 0
seen = []
for x in range(1, self.sheet.max_row):
Id = self.sheet.cell(row = x, colun = 1).value
seen.append(Id)
for file in files:
counter += 1
if file not in seen:
break
im = file
im = Image.open(im)
return im
def imageResize(self, image, constraint):
actualSize = [image.height, image.width]
if actualSize[0] > actualSize[1]:
factor = constraint[0]/actualSize[0]
else:
factor = constraint[1]/actualSize[1]
newSize = [value * factor for value in actualSize]
image.thumbnail(tuple(newSize))
return image
def getStep(self):
return 0
def drawTempLine(self, event):
self.im.paste(self.pixIm, (event.x - 1, event.y - 1, event.x + 1, event.y + 1))
app = Tk()
App(app)
app.mainloop()
這導致以下錯誤:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Alec\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:/Users/Alec/Contrast App.py", line 146, in drawTempLine
self.im.paste(self.pixIm, (event.x - 1, event.y - 1, event.x + 1, event.y + 1))
File "C:\Users\Alec\Anaconda3\lib\site-packages\PIL\ImageTk.py", line 177, in paste
im.load()
AttributeError: 'PhotoImage' object has no attribute 'load'
這是什麼屬性錯誤的原因,以及如何能說的對嗎?
請發佈錯誤。另外,請將代碼量減少到[mcve]。如果問題出在對象上的方法上,我們實際上只需要該對象,並且有足夠的代碼來複制錯誤。 –