从 scikit 数组转换为 PIL photoimage 的图像被扭曲
Image converted from a scikit array to PIL photoimage is being distorted
我正在尝试将由 scikit-image 和 scipy 处理过的图像添加到 tkinter gui。要将其添加到 canvas,需要将其另存为 png,或转换为 PIL
图像。但是,当我尝试使用 ImageTk
的 Image.fromarray()
时,它会严重扭曲图像。我不想将其保存为 png,因为它只是生成数据标签的中间步骤。
我试过检查数组的形状,它们是一样的。我试着打印出图像,filled_objects 是正确的图像,而 im
是扭曲的。所以这在 Tkinter gui 中不是问题。此外,如果我不使用 np.asarray()
,它会产生相同的输出。
def generateCanny(imageName):
#imagename should be a path to the image, created with os path join
img = skimage.io.imread(imageName)
print('orig {}'.format(img.shape))
gray = np.sqrt((img*img).sum(-1))
#converts the image to greyscale
edges = skimage.feature.canny(gray, sigma=3)
fill = scipy.ndimage.binary_fill_holes(edges)
return fill
imageName = os.path.join(imagePath, imageStr)
filled_objects = generateCanny(imageName)
a = np.asarray(filled_objects)
im = PIL.Image.fromarray(a)
这是两张图,左边是im
,右边是filled_objects
我认为您可以轻松转换它,因为 filled_objects
只是一个数组,但 Image.fromarray()
必须进行一些处理。
问题是 fromarray
没有正确解释布尔数组 a
。如果您将 a
转换回 RGB:
# Extend the array into 3 dimensions, repeating the data:
a = np.repeat(a[...,None],3,axis=2).astype(np.uint8)
# Scale to 0-255:
a = 255*a
im = PIL.Image.fromarray(a)
然后 im.show()
将显示正确的图像。
将结果转换为 NumPy 的 uint8
即可:
from skimage import data, color, feature, util
import tkinter as tk
import numpy as np
from PIL import ImageTk, Image
from scipy.ndimage import binary_fill_holes
rgb = data.hubble_deep_field()
gray = color.rgb2grey(rgb)
edges = feature.canny(gray, sigma=3)
filled_objects = binary_fill_holes(edges)
img_bool = Image.fromarray(filled_objects)
img_uint8 = Image.fromarray(util.img_as_ubyte(filled_objects))
root = tk.Tk()
photo_bool = ImageTk.PhotoImage(img_bool)
photo_uint8 = ImageTk.PhotoImage(img_uint8)
label_bool = tk.Label(root, image=photo_bool).grid(row=1, column=1)
label_uint8 = tk.Label(root, image=photo_uint8).grid(row=1, column=2)
root.mainloop()
我正在尝试将由 scikit-image 和 scipy 处理过的图像添加到 tkinter gui。要将其添加到 canvas,需要将其另存为 png,或转换为 PIL
图像。但是,当我尝试使用 ImageTk
的 Image.fromarray()
时,它会严重扭曲图像。我不想将其保存为 png,因为它只是生成数据标签的中间步骤。
我试过检查数组的形状,它们是一样的。我试着打印出图像,filled_objects 是正确的图像,而 im
是扭曲的。所以这在 Tkinter gui 中不是问题。此外,如果我不使用 np.asarray()
,它会产生相同的输出。
def generateCanny(imageName):
#imagename should be a path to the image, created with os path join
img = skimage.io.imread(imageName)
print('orig {}'.format(img.shape))
gray = np.sqrt((img*img).sum(-1))
#converts the image to greyscale
edges = skimage.feature.canny(gray, sigma=3)
fill = scipy.ndimage.binary_fill_holes(edges)
return fill
imageName = os.path.join(imagePath, imageStr)
filled_objects = generateCanny(imageName)
a = np.asarray(filled_objects)
im = PIL.Image.fromarray(a)
这是两张图,左边是im
,右边是filled_objects
我认为您可以轻松转换它,因为 filled_objects
只是一个数组,但 Image.fromarray()
必须进行一些处理。
问题是 fromarray
没有正确解释布尔数组 a
。如果您将 a
转换回 RGB:
# Extend the array into 3 dimensions, repeating the data:
a = np.repeat(a[...,None],3,axis=2).astype(np.uint8)
# Scale to 0-255:
a = 255*a
im = PIL.Image.fromarray(a)
然后 im.show()
将显示正确的图像。
将结果转换为 NumPy 的 uint8
即可:
from skimage import data, color, feature, util
import tkinter as tk
import numpy as np
from PIL import ImageTk, Image
from scipy.ndimage import binary_fill_holes
rgb = data.hubble_deep_field()
gray = color.rgb2grey(rgb)
edges = feature.canny(gray, sigma=3)
filled_objects = binary_fill_holes(edges)
img_bool = Image.fromarray(filled_objects)
img_uint8 = Image.fromarray(util.img_as_ubyte(filled_objects))
root = tk.Tk()
photo_bool = ImageTk.PhotoImage(img_bool)
photo_uint8 = ImageTk.PhotoImage(img_uint8)
label_bool = tk.Label(root, image=photo_bool).grid(row=1, column=1)
label_uint8 = tk.Label(root, image=photo_uint8).grid(row=1, column=2)
root.mainloop()