【问题标题】:Is there a way of displaying a 16-bit image in Tkinter?有没有办法在 Tkinter 中显示 16 位图像?
【发布时间】:2020-03-06 13:13:30
【问题描述】:

我得到了一个单层 uint16 tiff 图像,即 2d 数组,每个值的范围在 0-65355 之间。 当我使用 cv2 读取和显示图像时,它可以正常工作。

im = cv2.imread(path, -1)
cv2.imshow('im', im)

现在我正在尝试使用 Tkinter 制作一个 GUI,并将图像合并到 GUI 中,但它显示的只是一个白色画布。

im = cv2.imread(path, -1)
height = im.shape[0]
width = im.shape[1]
canvas.config(width=width, height=height)
image = Image.fromarray(im)
root.photo = photo = ImageTk.PhotoImage(image=image)
canvas.create_image(0, 0, image=photo, anchor=tk.NW)

其他类型的 RGB 8 位图像显示为 shuold。 我怎样才能显示图像?我做错了什么?

【问题讨论】:

标签: python numpy tkinter python-imaging-library cv2


【解决方案1】:

我最近遇到了同样的问题,但没有解决方案。我认为这是导致问题的原因。

您的图像mode 是I;16L 类型,但是当您调用ImageTk.PhotoImage‍ 时,构造函数会检查该类型是否属于["1", "L", "RGB", "RGBA"] 类型。既然不是,它会获取您的类型的基本模式(在我的情况下是“L”)并在 PhotoImage 类的 paste 函数中转换为该基本模式。

我不知道如何停止/绕过这个转换,所以我打算做的是显示图像的近似版本。我将把我的图像转换为标准的 8 位 RGB,然后在 Tkinter GUI 中显示,但将对原始图像进行任何操作。

编辑:我的工作/解决方案

因为我的图像是I;32L 类型,所以我使用 OpenCV 打开我的图像并使用 NumPy 将其转换为适当的类型。这是我的例子:

my_image = cv2.imread("image.tiff") # read in the image with OpenCV
my_image = np.uint8(my_image) # convert image into an unsigned 8 bit int array with NumPy
my_image = Image.fromarray(my_image) # read in image from array with Tkinter
rendered_image = ImageTk.PhotoImage(my_image) # create the photo image for display

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2011-04-17
    • 2020-11-06
    相关资源
    最近更新 更多