python - 调整图像大小 Python Tkinter
问题描述
您好,我在调整图片大小时遇到问题。我正在尝试调整图像大小以适合蓝色绘图。但是我这样做的方式会返回错误。
File "gui.py", line 42, in fileDialog
self.display = Label(image=self.photo.resize((800, 600),Image.ANTIALIAS))
AttributeError: 'PhotoImage' object has no attribute 'resize
我只是在测试它是否适合做 800,600 我真的不知道。
def fileDialog(self):
self.filename = filedialog.askopenfilename(title="Select")
self.label = ttk.Label(self.labelFrame, text="")
self.label.grid(column=1, row=2)
self.label.configure(text=self.filename)
self.photo= ImageTk.PhotoImage(file = self.filename)
self.display = Label(image=self.photo.resize((800, 600),Image.ANTIALIAS))
self.display.grid(row=0)
有什么我做错了吗?请指教。
解决方案
您需要调整图像的大小,而不是照片图像。
import tkinter as tk
from PIL import Image, ImageTk
filename = 'bell.jpg'
img = Image.open(filename)
resized_img = img.resize((200, 100))
root = tk.Tk()
root.photoimg = ImageTk.PhotoImage(resized_img)
labelimage = tk.Label(root, image=root.photoimg)
labelimage.pack()
要解决新问题,您不必在创建标签时知道文件名。以下代码产生相同的结果:
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
labelimage = tk.Label(root)
labelimage.pack()
filename = 'bell.jpg'
img = Image.open(filename)
resized_img = img.resize((200, 100))
root.photoimg = ImageTk.PhotoImage(resized_img)
labelimage.configure(image=root.photoimg)
推荐阅读
- java - 同时添加和排序到 ArrayList
- java - 为什么我的子实体在外键/主键中有一个空值?
- r - 标记组内重复测量的异常值(假设有数千个组)
- python-3.x - 以下数据是否为 ASCII 格式?如果不是,我将如何使用 python3 将其转换为 ASCII 字符
- c - C 中的循环意外运行
- python - 停止 pd.read_html 将类似科学记数法的字符串“0E10”转换为浮点 0.0
- java - 流可以创建新对象吗?
- python - `not` 运算符与 python 列表的奇怪行为
- java - 在核心上垂直多个实例
- python - 计算单个字符串中 char 的出现次数