【问题标题】:How to put image uploaded in tkinter into a function?如何将在 tkinter 中上传的图像放入函数中?
【发布时间】:2020-05-23 10:57:27
【问题描述】:

我正在尝试创建一个 Python tkinter 应用程序,用户可以在其中从文件上传图像,并且图像通过图像分割函数输出 matplotlib 图。

我有图像分割功能,它需要两个参数:神经网络,图像文件路径。

from torchvision import models
from PIL import Image
import matplotlib.pyplot as plt
import torch
import torchvision.transforms as T
import numpy as np


fcn = models.segmentation.fcn_resnet101(pretrained=True).eval()


# Define the helper function
def decode_segmap(image, nc=21):

  label_colors = np.array([(0, 0, 0),  # 0=background
               # 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle
               (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128),
               # 6=bus, 7=car, 8=cat, 9=chair, 10=cow
               (0, 128, 128), (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0),
               # 11=dining table, 12=dog, 13=horse, 14=motorbike, 15=person
               (192, 128, 0), (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128),
               # 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor
               (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128)])

  r = np.zeros_like(image).astype(np.uint8)
  g = np.zeros_like(image).astype(np.uint8)
  b = np.zeros_like(image).astype(np.uint8)

  for l in range(0, nc):
    idx = image == l
    r[idx] = label_colors[l, 0]
    g[idx] = label_colors[l, 1]
    b[idx] = label_colors[l, 2]

  rgb = np.stack([r, g, b], axis=2)
  return rgb


def segment(net, path):
  img = Image.open(path)
  plt.imshow(img); plt.axis('off'); plt.show()
  # Comment the Resize and CenterCrop for better inference results
  trf = T.Compose([T.Resize(256), 
                   T.CenterCrop(224), 
                   T.ToTensor(), 
                   T.Normalize(mean = [0.485, 0.456, 0.406], 
                               std = [0.229, 0.224, 0.225])])
  inp = trf(img).unsqueeze(0)
  out = net(inp)['out']
  om = torch.argmax(out.squeeze(), dim=0).detach().cpu().numpy()
  rgb = decode_segmap(om)
  plt.imshow(rgb); plt.axis('off'); plt.show()

我没有制作 Tkinter GUI,因为我不确定如何获取从文件上传的图像并将其转换为文件路径(字符串)并将其放入函数中。目前只有一个神经网络,即fcn。

【问题讨论】:

  • 你可以使用from tkinter import filedialog,然后用它来选择图像文件的路径path = filedialog.askopenfilename( initialdir='/Downloads', title='Select Photo', filetypes=(('JPEG files', '*.jpg'), ('PNG files', '*.png')))文件扩展名是jpeg nd png,你可以看到并且区分大小写
  • @CoolCloud 嘿,感谢您的推荐。我查看了文档,但我仍然有点困惑,我必须如何处理 filedialog 才能将文件路径作为变量返回,以便我可以通过函数传递它?再次感谢编辑:刚刚看到您的编辑

标签: python numpy tkinter pytorch


【解决方案1】:

首先导入filedialog和PIL:

from tkinter import filedialog
from PIL import Image

现在使用变量路径(或任何东西)来定义当您在 GUI 内选择时返回的路径。

path = filedialog.askopenfilename(initialdir='/Downloads', title='Select Photo', filetypes=(('JPEG files', '*.jpg'), ('PNG files', '*.png')))

您可以使用任何您想要的文件类型,方法是在参数中指定标题和“*.extension”。 请注意,它是区分大小写的。现在您的变量 path 具有图像的路径,您所要做的就是使用 PIL 打开它

img = Image.open(path)

就像您在代码中所做的那样

【讨论】:

  • 欢迎来到 SO,快乐编码 :)
  • 嘿,我一直在试验这个程序,但是,我无法通过分割函数解析文件路径,因为它无法从绑定到按钮的函数中获取图像路径用户的形象。我尝试过全局变量和类,但没有成功。提前致谢。
  • 尝试其他方法?你知道如果你命令一个按钮的功能,那么你必须使用 lamda 对吗?希望你已经做到了,如果仍然存在问题,那么只需尝试其他方法而不将其作为参数传递可能,我没有进入 matplotlib,所以我真的不知道你在做什么,但图像路径我在我的代码中使用了它
  • 谢谢你,我一定会去尝试添加 lambda。
  • 我和你一样感兴趣,请告诉我 :) 有没有办法用 tkinter 看到你的代码?
猜你喜欢
  • 2022-01-03
  • 2021-10-30
  • 2021-07-12
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
相关资源
最近更新 更多