Feathered edges on image with Pillow

后端 未结 2 1131
一个人的身影
一个人的身影 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:09

    Providing modified solution with gradient paste mask (as requested by @Crickets).

    from PIL import Image
    from PIL import ImageDraw
    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 paste mask
    mask = Image.new('L', back.size, 0)
    draw = ImageDraw.Draw(mask)
    x0, y0 = 0, 0
    x1, y1 = back.size
    for d in range(diam+RADIUS):
        x1, y1 = x1-1, y1-1
        alpha = 255 if d

    Paste mask:

    Result:

提交回复
热议问题