I'm making a program to get the outline of an image. So far, this is the output:
I want it to be only black and white. I need to iterate through every pixel and if the luminescence of the pixel is above a certain amount, set that pixel to pure white and otherwise set it to black.
I've only recently started learning about PIL so I'm not yet sure how to iterate through the pixels, or check their luminescence.
Asked
Active
Viewed 390 times
0

Jacob_
- 247
- 2
- 13
-
Take a look at [this question](http://stackoverflow.com/questions/6485254/how-to-i-use-pil-image-pointtable-method-to-apply-a-threshold-to-a-256-gray-im) and the Pillow docs for [image.point](http://pillow.readthedocs.org/en/3.1.x/reference/Image.html?highlight=.point#PIL.Image.Image.point) – PM 2Ring Feb 15 '16 at 11:53
1 Answers
0
Answering your question literally, you can use PIL
to apply a function to each of your pixels.
from PIL import Image
image = Image.open('pawn.png')
thresh = 120 # Change at your discretion
image = image.point(lambda i: 255 if i > thresh else 0)
This will produce a black background and white outline. If you want the reverse, simply swap the 255 and 0 in the last line.
If you are wanting to find the outlines of this object with more detail, you might want to look up how to sharpen and apply canny edge detection to the image. For those types of operations, I would recommend skimage (scikit-image) or opencv2

Mark Hannel
- 767
- 5
- 12