2012-11-17 84 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