简体   繁体   English

如何使用 PIL 模块调整 Tkinter 中的图像大小

[英]How can i resize images in Tkinter using PIL module

i got this code and i get this error: AttributeError: 'PhotoImage' object has no attribute 'resize' I have also tried rgrapg = Image.open("risinggrap.jpg") and i get this error: _tkinter.TclError: image "" doesn't exist我得到了这个代码,我得到了这个错误: AttributeError: 'PhotoImage' object has no attribute 'resize' 我也试过rgrapg = Image.open("risinggrap.jpg")我得到这个错误:_tkinter.TclError: image " "不存在

   rgraph = ImageTk.PhotoImage(Image.open("risinggrap.jpg"))
   rgraph = rgraph.resize((200,250),Image.ANTIALIAS)
   photoLabe = Label(x, image=rgraph)```


You need to load the image first, resize it, then cast it into ImageTk.PhotoImage .您需要先加载图像,调整其大小,然后将其投射到ImageTk.PhotoImage中。 Here is a working example that goes like so:这是一个工作示例,如下所示:

import tkinter as tk
from PIL import Image, ImageTk

x = tk.Tk()

# 1. load image
image = Image.open("risinggrap.jpg")

# 2. resize it
image = image.resize((200, 250), Image.ANTIALIAS)

# 3. cast it into ImageTk.PhotoImage
rgraph = ImageTk.PhotoImage(image)

photoLabel = tk.Label(x, image = rgraph)
photoLabel.pack(side = "bottom", fill = "both", expand = "yes")
x.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM