How to I use PIL Image.point(table) method to appl

2019-02-08 11:48发布

I have 8-bit greyscale TIFF images that I want to convert to Monochrome using a 75% white (decimal 190) threshold. In the Image.convert(mode) method section, the PIL manual says:

"When translating a greyscale image into a bitlevel image (mode "1"), all non-zero values are set to 255 (white). To use other thresholds, use the point method."

The Image.point(table) method says that it maps each pixel through the given table.

im.point(table, mode) => image
im.point(function, mode) => image

"Map the image through table, and convert it on fly. In the current version of PIL , this can only be used to convert 'L' and 'P' images to '1' in one step, e.g. to threshold an image."

How do I create the table (or function) that corresponds to the 75% threshold I need?

2条回答
爷的心禁止访问
2楼-- · 2019-02-08 12:28

Try im.point(lambda p: p > 190) and post the results.

查看更多
Bombasti
3楼-- · 2019-02-08 12:30

I found the complete solution in this answer "Write TIFF file in python from String". The function must include "and 255"

threshold = 191  
im = im.point(lambda p: p > threshold and 255)  
查看更多
登录 后发表回答