簡體   English   中英

Tkinter canvas 圖像錯誤

[英]Tkinter canvas image bug

我正在以 oop 樣式重寫我的應用程序,但遇到了意外問題。 調色板圖像失真。 這是以前從未發生過的。

在此處輸入圖像描述

class 集裝箱

class MainApp(Frame):
    def __init__(self, master, path):
        self.master = master
        Frame.__init__(self, self.master)

        self.configure_main()

        coloring = Coloring(self.master, path)
        coloring.grid(row=1, column=1)

從中實例化顏色選擇器實例的 class

class Coloring(Frame):  
    def __init__(self, parent, path):
        self.parent = parent
        Frame.__init__(self, self.parent)

        ...
        
        self.create_widgets()
        self.draw_widgets()
  
    def change_custom_color(self, *args):
        try:
            self.selector_frame.destroy()
        except AttributeError:
            pass

        self.selector_frame = ColorSelector(self.parent.master, args[1], self)
        self.selector_frame.grid(row=1, column=0)

顏色選擇器 class

class ColorSelector(Frame):
    def __init__(self, parent, btn_idx, coloring_obj):
        self.parent = parent
        Frame.__init__(self, self.parent)

        self.btn_idx = btn_idx

        self.palette_img_np = cv2.imread('resources/palette.png')
        self.palette_img_tk = cv2pil_images(self.palette_img_np)

        self.coloring_obj = coloring_obj

        self.create_widgets()
        self.draw_widgets()

    def create_widgets(self):
        self.palette = Canvas(self, width=253, height=253)
        self.palette.create_image(128, 3, anchor='n', image=self.palette_img_tk)
        self.palette.create_oval(5, 3, 251, 251, outline='black', width=4)
        self.cursor_obj_id = self.palette.create_oval(81, 81, 71, 71, fill='green', outline='white')
        self.palette.bind("<B1-Motion>", lambda event, arg=self.btn_idx: self.cursor_move(event, arg))

        self.slider_explanation = Label(self, text='Color saturation:')

        self.enchance_var = IntVar(value=1.0)
        self.enhance_slider = Scale(
            self, from_=0.1, to=1.0, orient=HORIZONTAL,
            command=lambda event, arg=self.btn_idx: self.change_enhance(event, arg),
            resolution=0.0001, variable=self.enchance_var, length=200
        )

        self.ok_btn = Button(self, text='OK', command=self.destroy)

    def draw_widgets(self):
        self.palette.pack(padx=15)
        self.slider_explanation.pack()
        self.enhance_slider.pack()
        self.ok_btn.pack(pady=10)

沒有錯誤的截圖在此處輸入圖像描述

先感謝您。

問題可能與數組轉換或其他問題有關,建議使用PIL本身加載和使用圖像,這樣更容易。 作為一種解決方法,您可以使用cv2.imwrite()並保存圖像,然后使用該路徑並使用PIL打開新圖像。 就像是:

# All the other processes...
path = 'img1.png' 
cv2.imwrite(path)

img = PIL.ImageTk.PhotoImage(Image.open(file))

並使用img作為圖像等。

這可能是您的陣列的一些錯誤,因為它對我來說不可重現,它工作得很好,無論如何這里是我會使用的 function:

def cv2pil(array):
    img = cv2.cvtColor(array,cv2.COLOR_BGR2RGB) # Also try COLOR_BGR2RGBA for png?
    pil_img = ImageTk.PhotoImage(Image.fromarray(img))
    return pil_img

img = cv2.imread('capture.png')
pil = cv2pil(img)

除了顏色模式切換,我看不到任何其他失真。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM