• 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()
    
  • 相关阅读:
    Delphi中解析Xml的控件-SimDesign NativeXml
    DELPHI判断是否64位操作系统
    几个获取Windows系统信息的Delphi程序
    delphi假死线程堵塞解决办法
    Delphi ADO数据操作封装类
    Delphi的时间与字符串函数代码示例
    【BZOJ2132】圈地计划 最小割
    【BZOJ3544】[ONTAK2010]Creative Accounting 前缀和+set
    【BZOJ4281】[ONTAK2015]Związek Harcerstwa Bajtockiego LCA
    【BZOJ2083】[Poi2010]Intelligence test 二分
  • 原文地址:https://www.cnblogs.com/zjutzz/p/13026200.html
Copyright © 2020-2023  润新知