2

I'm trying display openCV image in Tkinter label. When I load image from file like:

img = ImageTk.PhotoImage("asd.png")

everythink works fine. But when I change it, I get only empty, black image, anythink else. This is example code:

import tkinter as tk
from tkinter import *

import cv2
from skimage import io
from PIL import Image, ImageTk
from skimage.color import rgb2gray


def make_label(master, x, y, w, h, img, *args, **kwargs):
   f = Frame(master, height = h, width = w)
   f.pack_propagate(0)
   f.place(x = x, y = y)
   label = Label(f, image = img, *args, **kwargs)
   label.pack(fill = BOTH, expand = 1)
   return label


if __name__ == '__main__':
   root = tk.Tk()
   frame = tk.Frame(root, width=400, height=600, background='white')
   frame.pack_propagate(0)
   frame.pack()
   image = rgb2gray(io.imread("asd.png"))

   image2 = Image.fromarray(image)
   img = ImageTk.PhotoImage(image2)
   make_label(root, 0, 0, 100, 100, img)
   root.mainloop()

Please, help

1 Answer 1

2

I think the problem is that the greyscale values given by rgb2gray(io.imread("asd.png")) are between 0 and 1 while PIL expects values between 0 and 255, so the result of Image.fromarray(image) is a black square since all values are smaller than 1.

Therefore image2 = Image.fromarray(image*255) should give the expected image.

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

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.