2012-11-17 55 views
2

我想将numpy数组转换为PIL格式,然后将其显示为标签!我可以为我的原始图像执行此操作,但是在采用fft和fftshift后,我无法正确显示。!ImageTk.PhotoImage无法正常显示图像

image1=Image.open('sam.jpg') 
image1 = image1.resize((120, 120), Image.ANTIALIAS) 
Labe(image=photo1).grid(row=0, column=7, columnspan=3, rowspan=2, padx=5, pady=5) 
ipx1=np.array(image1) 
(w,h)=ipx1.shape #120x20 

现在我做一些东西与我的形象:但

img_fft=np.fft.fft2(image1) 
img_shift=np.fft.fftshift(image1_fft) 
img_plot1=np.log10(1+abs(img_shift)) 


foto=Image.fromarray((img_plot1*255.999).round().astype(np.uint8),mode='L') 
photo=ImageTk.PhotoImage(foto) 
Label(image=photo).grid(row=0, column=10, columnspan=4, rowspan=2, padx=5, pady=5) 

代替: correct imag

我越来越:

wrong image

什么想法?

回答

2

当您将东西重新投影到uint8 s时,您遇到溢出问题。

你跟(img_plot1*255.999).round().astype(np.uint8)转换,但是这会溢出附近或1(任何大于0.998时)

假设img_plot1总是包含在0和1之间的值的值,我觉得你的意思只是做或者:

(img_plot1 * 255.99).astype(np.uint8) 

(img_plot1 * 255).round().astype(np.uint8) 

round通话将全面上涨或下跌,而一个纯粹的int投射实际上是一个floor调用。

但是,只是从输出图像中的“波段”中猜测,输入数据溢出并“多次包装”。因此,您的输入数据可能具有比0-1更大的范围。

rescaled = 255 * (img_plot1 - img_plot1.min())/img_plot1.ptp() 
foto = Image.fromarray(rescaled.astype(np.uint8), mode='L') 

您还可以使用np.digitize到:

因此,如果你不想在img_plot1担心值的确切范围,你可能只是它基于其范围内重新调整到0-255重新调整数组,但它会导致可读性降低,imo如果要剪切高于和低于阈值的值(例如0和255),还可以查看np.clip

+0

非常感谢。重新调整帮助了我! – Moj