2017-04-17 66 views
0

我試圖建立一個非常簡單(甚至沒有用處)圖像去噪,但我有這個特定的代碼問題:爲什麼我會收到「ValueError:錯誤的條目數」?

im=Image.open("Test.jpg") 
width=im.size[0] 
Lim=im.convert("L") 
threshold = 250 
table = [] 
for i in range(width): 
    if i < threshold: 
     table.append(0) 
    else: 
     table.append(1) 
Bim=Lim.point(table, "1") 
Bim.save("BINvalue.bmp","BMP") 

它給我這個錯誤:

ValueError: Wrong number of lut entries 

我錯過了很簡單的東西嗎?或者是整件事情都錯了?我仍然是一名學生,並沒有太多的Python經驗。

回答

1

Image.point()方法採用查找表或函數來對每個像素進行操作。 查找表可能有點複雜。所以推薦使用功能。該功能適用​​於每個像素。

from PIL import Image 
im=Image.open("Test.jpg") 
width=im.size[0] 
Lim=im.convert("L") 
threshold = 250 
# if pixel value smaller than threshold, return 0 . Otherwise return 1. 
filter_func = lambda x: 0 if x < threhold else 1 
Bim=Lim.point(filer_func, "1") 
Bim.save("BINvalue.bmp","BMP") 
相關問題