Image outline using python/PIL

前端 未结 2 386
鱼传尺愫
鱼传尺愫 2021-01-31 10:43

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

2条回答
  •  鱼传尺愫
    2021-01-31 11:17

    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

提交回复
热议问题