shn
shn

Reputation: 5276

Convert a gray scal imageto bilevel

I'm trying to convert an image to a binary image (bilevel) using the method "point" of PIL as following:

def mappoint (i):
   if i > 1: return 1
   else: return 0

if __name__=="__main__":
   img = Image.open('no.tif')
   img = img.point(mappoint)
   img.save('ok.tif')

but it gives me a black image !

EDIT: If I do def mappoint (i): if i > 1: return 255 then it gives a all white image ! and if I print the value of i in mappoint it shows values from 0 to 255 (like if you do print range(256)), so each i is not the value of each pixels, is it ? There is another way to make the bilevel just by using convert when we open the image: img = Image.open(img_name).convert('1') but by default the threshold used is 127, and the doc says to use point method if we want another threshold

Upvotes: 0

Views: 1193

Answers (1)

mathematical.coffee
mathematical.coffee

Reputation: 56935

The function did work, you've generated an image with 0s and 1s. The reason it looks black is because standard images have intensities from 0 to 255.

An intensity of 1 such as you've done looks black because on a scale of 0 to 255, 1 is very close to 0.

Change your mappoint to do if i>1: return 255 if you wanted a black and white image.

Upvotes: 1

Related Questions