图片自动缩放,显示效果如下:
使用Tkinter处理图片,我们通常使用pillow模块(aks PIL)。它提供了加载,处理,转换等功能。可以。
使用PIL模块,一般按照以下步骤,修改图片大小:
-
安装pillow模块,使用pip install pillow 命令安装模块
-
模块安装后,可以导入模块 from PIL import Image, ImageTk
-
打开图片open(image___cpLocation)
-
resize((w,h), Image. ANTIALIAS)
-
ImageTk.PhotoImage()
-
更新控件,刷新PhotoImage对象
图片自动缩放原理:设置图片为Label背景图,Label绑定处理事件的函数。拖动改变窗口尺寸,随着Label尺寸的改变,在事件函数中修改图片尺寸。
初始化image和label,绑定resize_image函数
self.default_qr_image = Image.open(os.path.join(os.getcwd(),"xxx.png"))
self.default_qr_photo_image = ImageTk.PhotoImage(self.default_qr_image)
self.default_qr_image_label = tk.Label(rightArea, bg="black", image=self.default_qr_photo_image)
self.default_qr_image_label.pack(fill=tk.BOTH, expand=True)
# 绑定函数
self.default_qr_image_label.bind('<Configure>', self.resize_image)
resize_image函数,判断控件宽度和高度的最小值,设置为正方形图片的边长。代码:
def resize_image(self, event):
new_width = event.width
new_height = event.height
new_length = new_height if new_width > new_height else new_width
img_copy = self.default_qr_image.resize((new_length, new_length))
self.default_qr_photo_image = ImageTk.PhotoImage(img_copy)
self.default_qr_image_label.config(image=self.default_qr_photo_image)
完整代码参考:
import os
import tkinter as tk
import Constants
import qrcode
from plugins.PluginBase import PluginBase
from PIL import Image, ImageTk
DEFAULT_QR_IMAGE_NAME = "default_qr_img.png"
class qrcode(PluginBase):
default_qr_image_label = None
default_qr_image = None
default_qr_photo_image = None
def __init__(self, root=None):
super().__init__(root)
def create_widgets(self):
# convert text to image
text2ImageFrame = tk.Frame(self)
text2ImageFrame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
leftArea = tk.Frame(text2ImageFrame)
leftArea.place(anchor=tk.NW, relwidth=0.4, relheight=1)
midArea = tk.Frame(text2ImageFrame)
midArea.place(anchor=tk.NW, relwidth=0.2, relheight=1, relx=0.4)
rightArea = tk.Frame(text2ImageFrame)
rightArea.place(anchor=tk.NW, relwidth=0.4, relheight=1, relx=0.6)
# text2ImageFrame -> leftArea
tk.Label(leftArea, text=self.loc_helper.get_string("INPUT_TEXT")).pack(side=tk.TOP, anchor=tk.W)
input_text = tk.Text(leftArea)
scroll = tk.Scrollbar(leftArea)
# note: pack scroll first, then pack text
scroll.pack(side=tk.RIGHT, fill=tk.Y)
input_text.pack(side=tk.TOP)
scroll.config(command=input_text.yview)
input_text.config(yscrollcommand=scroll.set)
# text2ImageFrame -> midArea
btn = tk.Button(midArea, text=self.loc_helper.get_string("CREATE_IMAGE"))
btn.place(anchor=tk.CENTER, relx=0.5, rely=0.5)
# text2ImageFrame -> rightArea
self.default_qr_image = Image.open(os.path.join(os.getcwd(),
Constants.PLUGIN_DIR,
self.__class__.__name__,
DEFAULT_QR_IMAGE_NAME))
self.default_qr_photo_image = ImageTk.PhotoImage(self.default_qr_image)
self.default_qr_image_label = tk.Label(rightArea, image=self.default_qr_photo_image)
self.default_qr_image_label.pack(fill=tk.BOTH, expand=True)
self.default_qr_image_label.bind('<Configure>', self.resize_image)
# convert image to text
tk.Frame(self, bg="blue").pack(side=tk.TOP, fill=tk.BOTH, expand=True)
def resize_image(self, event):
new_width = event.width
new_height = event.height
new_length = new_height if new_width > new_height else new_width
img_copy = self.default_qr_image.resize((new_length, new_length))
self.default_qr_photo_image = ImageTk.PhotoImage(img_copy)
self.default_qr_image_label.config(image=self.default_qr_photo_image)
最后,使用Pillow改变图片大小,有几点需要注意:。
- 安装模块是pip install pillow,不是pip install PIL
- 使用模块是from PIL import Image, ImageTk,不是from pillow import Image, ImageTk
- resize参数是((width, heith)),不是(width, heith)