Imagetk.photoImage无法正确显示图像

发布于 2025-02-13 21:22:20 字数 532 浏览 4 评论 0原文

我想在tkinter GUI中显示.tif文件,但是我一直很难使图像正确加载。我能够使用image.poen ='r'正确查看图像,但是当我调用imagetk.photoimage时,它会显示一个空白的白色画布。这是代码:

def func(image_paths):
    root = Tk()
    im = Image.open(image_paths[0], mode='r')
    im.show()  # shows image correctly
    im = ImageTk.PhotoImage(im)
    im_label = Label(image=im)
    # im_label = Label(image=ImageTk.PhotoImage(Image.open(image_paths[0], mode='r')))  # This doesn't work either
    im_label.grid(row=0, column=0, columnspan=3)
    root.mainloop()

任何帮助都非常感谢!

I want to show a .tif file in a tkinter gui, but I have been having trouble getting the images to load properly. I was able to view the image correctly using Image.open with mode='r', but then when I call ImageTk.PhotoImage it displays a blank white canvas. Here is the code:

def func(image_paths):
    root = Tk()
    im = Image.open(image_paths[0], mode='r')
    im.show()  # shows image correctly
    im = ImageTk.PhotoImage(im)
    im_label = Label(image=im)
    # im_label = Label(image=ImageTk.PhotoImage(Image.open(image_paths[0], mode='r')))  # This doesn't work either
    im_label.grid(row=0, column=0, columnspan=3)
    root.mainloop()

Any help is much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的呼吸 2025-02-20 21:22:20

因此,最终工作的是该视频中的解决方案: https://www.youtube.com /watch?v = 6l6hdqywdh0
使其工作的代码线是:

curr = Image.open(image_paths[0], mode='r')
curr = Image.fromarray(np.uint8(cm.jet(NormalizeData(np.array(curr).astype(np.float64)))*255))
curr = ImageTk.PhotoImage(curr)

So, what ended up working was the solution in this video: https://www.youtube.com/watch?v=6l6HDQyWdH0
The lines of code that got it to work was:

curr = Image.open(image_paths[0], mode='r')
curr = Image.fromarray(np.uint8(cm.jet(NormalizeData(np.array(curr).astype(np.float64)))*255))
curr = ImageTk.PhotoImage(curr)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文