Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image outline using python/PIL

I have a color photo of apple, how can I show only its outline (inside white, background black) with python/PIL?

like image 459
user1212200 Avatar asked Feb 16 '12 22:02

user1212200


People also ask

How do I pad an image in PIL?

If you want to resize an image but do not want to change the aspect ratio or trim it, you can adjust the size by adding padding to the top, bottom, left, and right of the image. You can pad an image by using new() and paste() of the Python image processing library Pillow (PIL).

What is a PIL image Python?

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image.


2 Answers

Something like this should work.

from PIL import Image, ImageFilter

image = Image.open('your_image.png')
image = image.filter(ImageFilter.FIND_EDGES)
image.save('new_name.png') 

If that doesn't give you the result you are looking for then you try implementing either Prewitt edge detection, Sobel edge detection or Canny edge detection using PIL and Python and other libraries see related question and the following example .

If you are trying to do particle detection / analysis rather than just edge detection, you can try using py4ij to call the ImageJ method you link to give you expect the same result, or try another Particle Analysis Python library EMAN alternately you can write a Particle detection algorithm using PIL, SciPy and NumPy.

like image 179
Appleman1234 Avatar answered Oct 10 '22 12:10

Appleman1234


If your object and background have fairly well contrast

from PIL import Image
image = Image.open(your_image_file)
mask=image.convert("L")
th=150 # the value has to be adjusted for an image of interest 
mask = mask.point(lambda i: i < th and 255)
mask.save(file_where_to_save_result)

if higher contrast is in one (of 3 colors), you may split the image into bands instead of converting it into grey scale.

if an image or background is fairly complicated, more sophisticated processing will be required

like image 21
cur4so Avatar answered Oct 10 '22 12:10

cur4so