2017-10-28 1 views
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' 

내가 그것을하는 방법을이 속성 오류의 원인과 무엇을 보정 할 수있어 여기에

코드의 관련 부분입니까?

+0

오류를 게시하십시오. 또한 코드의 양을 [mcve]로 줄이십시오. 문제가 객체에 대한 메쏘드와 함께 있다면 우리는 실제로 그 객체와 오류를 복제 할 수있는 충분한 코드 만 필요로합니다. –

답변

0

Image에는 PhotoImage이 아닌 붙여 넣기 방법이 있습니다.

이미지를 수정 한 다음 PhotoImage로 다시 변환하여 이미지를 표시해야합니다.

우리가 실행할 수있는 것처럼 예를 보여주지 않으면 코드를 도울 수 없습니다.

관련 문제