【问题标题】:Using Tkinter to display images from a numpy array使用 Tkinter 显示来自 numpy 数组的图像
【发布时间】:2020-01-02 14:07:35
【问题描述】:

我对 Python 没有经验,并且第一次使用 Tkinter 制作一个 UI,显示我的数字分类程序与 mnist 数据集的结果。我有一个关于在 Tkinter 中显示来自 numpy 数组而不是我 PC 上的文件路径的图像的问题。我为此尝试的当前代码是:

    img = PhotoImage(test_images[0])
    window.create_image(20,20, image=img)

这是不成功的,但我不知道如何处理它。下面是从数组中绘制的图像的图片,我想在 UI 中显示,图像下方只是显示我如何加载和绘制图像的代码,以防万一。抱歉,如果这是一个我缺少的简单修复,我对此很陌生。干杯

https://i.gyazo.com/8962f16b4562c0c15c4ff79108656087.png

# Load the data set
train_images = mnist.train_images() #training data
train_labels = mnist.train_labels() #training labels
test_images = mnist.test_images() # training training images
test_labels = mnist.test_labels()# training data labels

# normalise the pixel values of the images to make the network easier to train
train_images = (train_images/255) - 0.5
test_images = (test_images/255) - 0.5
# Flatten the images in to a 784 dimensional vector to pass into the neural network
train_images = train_images.reshape((-1, 784))
test_images = test_images.reshape((-1, 784))
# Print shape of images
print(train_images.shape) # 60,000 rows and 784 columns
print(test_images.shape)
for i in range(0,15):
    first_image = test_images[i]
    first_image = np.array(first_image, dtype='float')
    pixels = first_image.reshape((28,28))
    plt.imshow(pixels)
    plt.show()

错误信息:

Traceback (most recent call last):
  File "C:/Users/Ben/Desktop/Python Projects/newdigitclassifier/classifier.py", line 122, in <module>
    img = PhotoImage(test_images[0])
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3545, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3491, in __init__
    if not name:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

【问题讨论】:

  • 尝试使用Pillow模块中的Image.fromarray(...)函数将数组转换为图像。

标签: python arrays tensorflow machine-learning tkinter


【解决方案1】:

解决办法如下:

import cv2
import tkinter as tk
from PIL import Image, ImageTk
import tensorflow as tf

# initializing window and image properties
HEIGHT = 200
WIDTH = 200
IMAGE_HEIGHT = 200
IMAGE_WIDTH = 200

# loading mnist dataset
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()

def imageShow(index):
    root = tk.Tk()
    # resizing image into larger image
    img_array = cv2.resize(x_train[index], (IMAGE_HEIGHT,IMAGE_WIDTH), interpolation = cv2.INTER_AREA)
    img =  ImageTk.PhotoImage(image=Image.fromarray(img_array))
    canvas = tk.Canvas(root,width=WIDTH,height=HEIGHT)
    canvas.pack()
    canvas.create_image(IMAGE_HEIGHT/2,IMAGE_WIDTH/2, image=img)
    root.mainloop()

imageShow(5)

数据集已从 tensorflow 导入。 我添加了一个额外的功能来调整图像大小。 还有the result looks like this

【讨论】:

  • 很高兴为您提供帮助,继续编码也确保给它​​一个赞成票,我是 stackoverflow 的新手
【解决方案2】:

仅依赖于 numpy 和 TkInter 的类似任务的实现可以在这里找到:

https://gist.github.com/FilipDominec/14761052f42d80d283bd3adcf7eb5347

这是一个“波纹坦克模拟器”示例,我尝试尽可能优化它的速度。

它还允许选择颜色图、浮雕、放大和缩小等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    相关资源
    最近更新 更多