Python 3.x 在Tkinter中调整图像大小

Python 3.x 在Tkinter中调整图像大小,python-3.x,tkinter,windows-10,python-imaging-library,Python 3.x,Tkinter,Windows 10,Python Imaging Library,所以,我需要在tkinter中调整图像的大小。在你做任何事情之前-这不是重复的。我已经在这个网站上浏览了所有其他问题,没有一个对我有帮助。我的问题是——我不想保存图像。我需要加载图像,调整其大小,然后在标签中显示它与PhotoImage。我尝试使用ImageTk,但由于某种原因它无法工作 以下是我的ImageTk代码: from tkinter import * import PIL from PIL import Image, ImageTk root = Tk() left_nose_p

所以,我需要在tkinter中调整图像的大小。在你做任何事情之前-这不是重复的。我已经在这个网站上浏览了所有其他问题,没有一个对我有帮助。我的问题是——我不想保存图像。我需要加载图像,调整其大小,然后在标签中显示它与PhotoImage。我尝试使用ImageTk,但由于某种原因它无法工作

以下是我的ImageTk代码:

from tkinter import *
import PIL
from PIL import Image, ImageTk

root = Tk()

left_nose_path_white = Image.open(r'C:\Users\User\Documents\Python stuff\Other apps\Veteris\Dog photos\Advanced\Sides\GIF\Nose\Nose (left) - White.gif')

def resize_image():
    global left_nose_path_white

    total_screen_width = root.winfo_screenwidth()
    total_screen_height = root.winfo_screenheight()

    frame_width, frame_height = left_nose_path_white.size

    dog_new_width = int(frame_width / 3)
    dog_new_height = int(frame_height / 3)

    left_nose_path_white = left_nose_path_white.resize((dog_new_width, dog_new_height), Image.LANCZOS)

resize_image()

left_nose_img_white = ImageTk.PhotoImage(file = left_nose_path_white)
label = Label(image = left_nose_img_white)
label.pack()

root.mainloop()
这将返回错误:

  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 124, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
我的代码应该找到原始图像的宽度和高度,将其除以3,然后显示它。我不想保存图像的原因是因为用户会多次打开应用程序。 我对使用避孕药/枕头还不熟悉,所以答案可能是显而易见的。 我用pip安装了枕头。 我的计算机上只有一个Python版本(Python 3.7)

完全错误:

Traceback (most recent call last):
  File "C:\Users\User\Documents\Python stuff\Other apps\Veteris\Scripts\Veteris_program.py", line 230, in <module>
    left_nose_img_white = ImageTk.PhotoImage(file = left_nose_path_white)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 95, in __init__
    image = _get_image_from_kw(kw)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 64, in _get_image_from_kw
    return Image.open(source)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2779, in open
    prefix = fp.read(16)
AttributeError: 'Image' object has no attribute 'read'
Exception ignored in: <function PhotoImage.__del__ at 0x000002B0A8A49950>
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageTk.py", line 124, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
回溯(最近一次呼叫最后一次):
文件“C:\Users\User\Documents\Python stuff\Other apps\Veteris\Scripts\Veteris\u program.py”,第230行,在
左鼻镜图像白色=图像tk.PhotoImage(文件=左鼻镜路径白色)
文件“C:\Users\User\AppData\Local\Programs\Python37\lib\site packages\PIL\ImageTk.py”,第95行,在uu init中__
图像=\u从\u获取\u图像\u功率(kw)
文件“C:\Users\User\AppData\Local\Programs\Python37\lib\site packages\PIL\ImageTk.py”,第64行,在\u get\u image\u from\u kw中
返回Image.open(源代码)
打开文件“C:\Users\User\AppData\Local\Programs\Python37\lib\site packages\PIL\Image.py”,第2779行
前缀=fp.read(16)
AttributeError:“图像”对象没有属性“读取”
在中忽略异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\User\AppData\Local\Programs\Python37\lib\site packages\PIL\ImageTk.py”,第124行,在__
name=self.\u photo.name
AttributeError:“PhotoImage”对象没有属性“\u PhotoImage\u photo”

谢谢你的帮助

ImageTk.PhotoImage(file=left\u nose\u path\u white)
替换为
ImageTk.PhotoImage(left\u nose\u path\u white)
@Mike-SMT噢哇!谢谢,我快疯了。有时候,这些简单的问题需要花费很长的时间才能解决:D。我曾经有过几次这样的事情发生在我身上。不过还是要给我一些建议。不要说“这不是复制品”。这只会让人们找到复制品并关闭你的帖子。事实上,这是一个重复的问题,已经被问了很多次。