简体   繁体   中英

How to displaying a numpy int16 array correctly with tkinter?

When loading an image into tkinter, when the image is stored as a numpy array as int16, the image displayed has pixel intensity thresholds for int8, meaning that everything looks awful (all values above 255 -which are plenty- are white). How can I set the min/max values for thresholding on tkinter?

import tkinter as tk
import numpy as np
from PIL import Image, ImageTk

root = tk.Tk()

array = np.reshape(np.array(range(0,200*200), dtype='int32'),(200,200))
img =  ImageTk.PhotoImage(image=Image.fromarray(array))

canvas = tk.Canvas(root,width=300,height=300)
canvas.pack()
canvas.create_image(20,20, anchor="nw", image=img)

root.mainloop()

This is how the image looks like:

在此处输入图片说明

This does not seem to be a PIL problem, as the 'image' stored in image seems to keep the original values:

image=Image.fromarray(array)
image

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM