tkinter缩放窗口和图像

当tkinter里的一个组件是图像时,如果允许缩放主窗口的同时,让图像也缩放,有时候是一种需求。

主要实现方法是bind一个回调函数。这里的例子是,图像上绘制了矩形框,当窗口被用鼠标缩放时,图像缩放,框也缩放。

# coding: utf-8

"""
Demonstrates how to resizing tkinter windows and image on it

https://cloud.tencent.com/developer/article/1430998

https://stackoverflow.com/questions/7299955/tkinter-binding-a-function-with-arguments-to-a-widget
"""

import tkinter as tk
from PIL import Image, ImageTk
import cv2

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.lbPic = tk.Label(self, text='test', width=400, height=600)
        
        self.im_orig = cv2.imread('E:/data/VOC2007/JPEGImages/000001.jpg')

        self.xmin_orig = 8
        self.ymin_orig = 12
        self.xmax_orig = 352
        self.ymax_orig = 498

        cv2.rectangle(
            self.im_orig,
            pt1 = (self.xmin_orig, self.ymin_orig),
            pt2 = (self.xmax_orig, self.ymax_orig),
            color = (0, 255, 0),
            thickness = 2
        )

        self.im_orig = self.im_orig[:, :, ::-1]  # bgr => rgb   necessary

        tkim = ImageTk.PhotoImage(Image.fromarray(self.im_orig))
        self.lbPic['image'] = tkim
        self.lbPic.image = tkim

        self.lbPic.bind('<Configure>', self.changeSize)
        self.lbPic.pack(fill=tk.BOTH, expand=tk.YES)

    def changeSize(self, event):
        im = cv2.resize(self.im_orig, (event.width, event.height))

        tkim = ImageTk.PhotoImage(Image.fromarray(im))
        self.lbPic['image'] = tkim
        self.lbPic.image = tkim


def main():
    app = App()
    app.title('缩放图像')

    app.mainloop()


if __name__ == '__main__':
    main()

不过在MacOSX下,上述代码显示有问题。改成如下:

import tkinter as tk
from PIL import Image, ImageTk
import cv2

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry("800x600")
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.lbPic = tk.Label(self, text='test', compound='center')

        self.im_orig = cv2.imread('/Users/chris/data/VOC2007/JPEGImages/000001.jpg')

        self.xmin_orig = 8
        self.ymin_orig = 12
        self.xmax_orig = 352
        self.ymax_orig = 498

        cv2.rectangle(
            self.im_orig,
            pt1 = (self.xmin_orig, self.ymin_orig),
            pt2 = (self.xmax_orig, self.ymax_orig),
            color = (0, 255, 0),
            thickness = 2
        )

        self.im_orig = self.im_orig[:, :, ::-1]  # bgr => rgb   necessary

        tkim = ImageTk.PhotoImage(Image.fromarray(self.im_orig))
        self.lbPic['image'] = tkim
        self.lbPic.image = tkim

        self.lbPic.bind('<Configure>', self.changeSize)
        self.lbPic.grid(row=0, column=0, sticky=tk.NSEW)

    def changeSize(self, event):
        im = cv2.resize(self.im_orig, (event.width, event.height))

        tkim = ImageTk.PhotoImage(Image.fromarray(im))
        self.lbPic['image'] = tkim
        self.lbPic.image = tkim


def main():
    app = App()
    app.title('缩放图像')

    app.mainloop()


if __name__ == '__main__':
    main()
posted @ 2020-06-01 17:13  ChrisZZ  阅读(3794)  评论(0编辑  收藏  举报