簡體   English   中英

為什么會收到“ ValueError:lut條目數量錯誤”的信息?

[英]Why am I getting “ValueError: Wrong number of lut entries”?

我正在嘗試構建一個非常簡單(甚至可能沒有用)的圖像去噪器,但是此特定代碼段存在問題:

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經驗。

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")

我們使用Image.point()函數將灰度圖像轉換為黑白圖像。該函數有兩個參數,第一個參數是確定轉換規則的查找表。該表將檢測圖像的每個像素的灰度值並根據閾值和灰度值之間的比較將值重新分配為1/0。

如果使用循環函數構造表,則需要將循環范圍限制為灰度值而不是圖像大小。您可以按以下方式更改循環函數:

for i in range(256): 
    if i < threshold:
        table.append(0)
    else:
        table.append(1)

也許你可以參考這個Image.point()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM