Feathered edges on image with Pillow

后端 未结 2 1142
一个人的身影
一个人的身影 2021-01-14 04:45

I\'m trying to figure out how to feather the edges of an image using Pillow with Python.

I need something like this cute cat (ignore the visible edges):

2条回答
  •  情书的邮戳
    2021-01-14 05:01

    Have a look at this example:

    from PIL import Image
    from PIL import ImageFilter
    
    RADIUS = 10
    
    # Open an image
    im = Image.open(INPUT_IMAGE_FILENAME)
    
    # Paste image on white background
    diam = 2*RADIUS
    back = Image.new('RGB', (im.size[0]+diam, im.size[1]+diam), (255,255,255))
    back.paste(im, (RADIUS, RADIUS))
    
    # Create blur mask
    mask = Image.new('L', (im.size[0]+diam, im.size[1]+diam), 255)
    blck = Image.new('L', (im.size[0]-diam, im.size[1]-diam), 0)
    mask.paste(blck, (diam, diam)) 
    
    # Blur image and paste blurred edge according to mask
    blur = back.filter(ImageFilter.GaussianBlur(RADIUS/2))
    back.paste(blur, mask=mask)
    back.save(OUTPUT_IMAGE_FILENAME)
    

    Original image (author - Irene Mei):

    Pasted on white background:

    Blur region (paste mask):

    Result:

提交回复
热议问题