简体   繁体   English

为什么会收到“ ValueError:lut条目数量错误”的信息?

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

I am trying to build a very simple (maybe not even useful) image denoiser, but I'm having issues with this particular piece of code: 我正在尝试构建一个非常简单(甚至可能没有用)的图像去噪器,但是此特定代码段存在问题:

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

It's giving me this error: 这给了我这个错误:

ValueError: Wrong number of lut entries

Am I missing something very simple? 我是否缺少一些简单的东西? Or is the whole thing wrong? 还是整件事错了? I am still a student and don't have a whole lot of experience in Python. 我仍然是一名学生,并且没有大量的Python经验。

The Image.point() method take a lookup table or a function to operate on every pixel. Image.point()方法采用一个查找表或一个对每个像素进行操作的函数。 Lookup table may be a little complicated. 查找表可能有点复杂。 So use a function is recommended. 因此,建议使用功能。 The function apply to every pixel. 该功能适用​​于每个像素。

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

We use Image.point() function to turn a gray image into black and white.The function has two arguments,the first argument is a lookup table that determines the transformation rules.The table will detect the gray value of each pixel of the image and reassign the value to 1/0 which depends on the comparison between the threshold and the gray value . 我们使用Image.point()函数将灰度图像转换为黑白图像。该函数有两个参数,第一个参数是确定转换规则的查找表。该表将检测图像的每个像素的灰度值并根据阈值和灰度值之间的比较将值重新分配为1/0。

If you use a loop function to construct a table, you need to limit your loop range to the amounts of gray values rather than the size of your image.You can alter your loop function like this: 如果使用循环函数构造表,则需要将循环范围限制为灰度值而不是图像大小。您可以按以下方式更改循环函数:

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

Maybe you can refer to this Image.point() 也许你可以参考这个Image.point()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM