Python TKinter在';这不是RGB 我有一些Tcter代码,在画布中间显示一个图像: class MyClass(): def __init__(self): self.root = tk.Tk() width=1280 height=720 self.testImage = Image.open("Drawing.png") self.canvas = tk.Canvas(self.root,height=height,width=width,bg='blue') # from some SO post basewidth=720 wpercent = (basewidth/float(self.testImage.size[0])) hsize = int((float(self.testImage.size[1])*float(wpercent))) self.testImage = self.testImage.resize((400,400),PIL.Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(self.testImage) self.canvas.create_image(width/2,height/2,image=self.photo) self.canvas.pack(side="top", fill="both", expand=True) 这显示我的图片在蓝色1280x720画布中间。如果我将“调整大小”更改为: self.testImage = self.testImage.resize((500,500),PIL.Image.ANTIALIAS)

Python TKinter在';这不是RGB 我有一些Tcter代码,在画布中间显示一个图像: class MyClass(): def __init__(self): self.root = tk.Tk() width=1280 height=720 self.testImage = Image.open("Drawing.png") self.canvas = tk.Canvas(self.root,height=height,width=width,bg='blue') # from some SO post basewidth=720 wpercent = (basewidth/float(self.testImage.size[0])) hsize = int((float(self.testImage.size[1])*float(wpercent))) self.testImage = self.testImage.resize((400,400),PIL.Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(self.testImage) self.canvas.create_image(width/2,height/2,image=self.photo) self.canvas.pack(side="top", fill="both", expand=True) 这显示我的图片在蓝色1280x720画布中间。如果我将“调整大小”更改为: self.testImage = self.testImage.resize((500,500),PIL.Image.ANTIALIAS),python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,我得到一张空的蓝色1280x720画布。我一直在努力使图像缩放以填充画布,但如果我的图像大小超过400x400,它就会消失。我的基本图像文件是3000x2000左右的PNG 问题:如果使用画布宽度/高度创建图像(…),请尝试以下操作: 考虑这个例子: 评论:…使用双线性,我可以在它消失之前将其扩展到800x720 考虑在脚本外部将图像向下采样至1280x720,而不是在脚本内部缩小大小 根据文档,不支持FilterPIL.Image.ANTIALIAS 返回此图像的已调整大小的副本。 参数:

我得到一张空的蓝色1280x720画布。我一直在努力使图像缩放以填充画布,但如果我的图像大小超过400x400,它就会消失。我的基本图像文件是3000x2000左右的PNG

问题:如果使用
画布宽度/高度
创建图像(…),请尝试以下操作:

考虑这个例子:


评论:…使用双线性,我可以在它消失之前将其扩展到800x720

考虑在脚本外部将图像
向下采样
至1280x720,而不是
在脚本内部缩小大小


根据文档,不支持Filter
PIL.Image.ANTIALIAS

返回此图像的已调整大小的副本。
参数:

大小–以像素为单位的请求大小,以2元组形式:(宽度、高度)。
重采样–可选的重采样过滤器。
这可以是PIL.Image.NEAREST、PIL.Image.BOX、PIL.Image.billine、PIL.Image.HAMMING、PIL.Image.BICUBIC或PIL.Image.LANCZOS中的一个。
如果省略,或者图像具有模式“1”或“P”,则设置为PIL.image.NEAREST.
请参阅:


我设法用找到的解决方案解决了这个问题

简单更改:
self.testImage=Image.open(“Drawing.png”)


self.testImage=Image.open(“Drawing.png”).convert(“RGB”)


允许它按预期运行。

如果我替换该语句,图像不会以任何大小显示在画布上。如果我将
ANTIALIAS
替换为
双线性
我可以在图像消失之前将图像缩放到800x720,因此您可能会发现一些问题。我的程序的目的是为用户处理任意照片,因此显示可能较大的图像是核心功能。使用
Image.show()
可以很好地显示图像,可以调整大小到任意大小,也可以根本不调整大小。我尝试在画布上放置一个较大的GIF图像,而不必使用
Image
类,只需使用
ImageTk
,效果与预期一样。
def __init__(self):
    self.root = tk.Tk()
    width=1280; height=720
    self.canvas = tk.Canvas(self.root,height=height,width=width,bg='blue')

    self.testImage = Image.open("Drawing.png")
    self.testImage = self.testImage.resize((width,height),PIL.Image.BILINEAR)

    width,height=self.testImage.size
    self.photo = ImageTk.PhotoImage(self.testImage)
    self.canvas.create_image(width/2,height/2,image=self.photo)
    self.canvas.pack(side="top", fill="both", expand=True)
Image.resize(size, resample=0)