2

i'm inexperienced with Python and using Tkinter for the first time to make a UI that displays results of my digit classification program with the mnist dataset. I have a question about displaying images in Tkinter when they're from a numpy array rather than a filepath on my PC. The current code I have tried for this is:

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

Which was unsuccessful, however i'm not sure how else to approach it. Below is a picture of the image plotted from the array that I would like to display in the UI, and below the image is just the code that shows how i'm loading and plotting the images in case that helps. Sorry if this is an easy fix that i'm missing, i'm very new to this. Cheers

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()

Error Message:

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()
1
  • Try to use Image.fromarray(...) function from Pillow module to convert the array to image. Commented Jan 2, 2020 at 14:55

2 Answers 2

5

Here is the solution:

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)

The dataset have been imported from tensorflow. I have added an extra feature to resize images. And the result looks like this

Sign up to request clarification or add additional context in comments.

1 Comment

happy to help you, keep coding also make sure to give it an upvote, I am new to stackoverflow
1

An implementation of a similar task that depends on numpy and TkInter only can be found here:

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

It is a "ripple tank simulator" example and I tried to optimize its speed as much as possible.

It also allows for choosing color map, embossing, zooming in and out etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.