简体   繁体   中英

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.

The Image.point() method take a lookup table or a function to operate on every pixel. 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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