Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I use PIL Image.point(table) method to apply a threshold to a 256 gray image?

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?

like image 636
tahoar Avatar asked Jun 26 '11 16:06

tahoar


People also ask

How do I set threshold value in Python?

Wand threshold() function – Python The threshold() function is an inbuilt function in the Python Wand ImageMagick library which is used to modify the image such that any pixel's intensity value greater than the threshold is assigned the maximum intensity (white), or otherwise is assigned the minimum intensity (black).

What is threshold value in image processing?

Term: ThresholdingThe threshold of image intensity (relative image lightness) is set manually at a specific value or automatically set by an application. Pixels below that set threshold value are converted to black (bit value of zero), and pixels above the threshold value are converted to white (a bit value of one).


2 Answers

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)  
like image 113
tahoar Avatar answered Sep 28 '22 03:09

tahoar


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

like image 26
pajton Avatar answered Sep 28 '22 04:09

pajton