1

I want to apply the feather effect to the edges of images.

For example:

example1

It is pretty simple to do with rectangular images. But I'm not able to find a way to do that on non-rectangular images (ones with transparent backgrounds).

Here is an example:

example 2

I have tried various things with opencv and PIL but no luck.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Z Dhillon
  • 21
  • 4
  • You have to have a mask image for an alpha channel. Then you blur the edges of the mask and use that in place of the original alpha channel. See my example of that in my answer at https://stackoverflow.com/questions/63001988/how-to-remove-background-of-images-in-python/63003020#63003020 – fmw42 Nov 19 '21 at 16:27

1 Answers1

1
import numpy as np
import cv2
# import the image
input_image = cv2.imread('VagcO.png')
# get the number of rows & columns in m & n respectively 
n , m = input_image.shape[0], input_image.shape[1]
# copy the image for image processing
output = np.copy(input_image)
# loop on RGB channels
for layer in range(3):
    value = 255
    # loop for bottom of image only 15 rows pixel
    for row in reversed(range(n)[n-15:]):
        # loop for every column pixel in that row
        for column in range(m):
            # checking if every pixel is zero for usualy for background of images
            if output[row,column,0]==0 and output[row,column,1]==0 and output[row,column,2]==0:
                # skip that pixel
                continue
            # chaning the value to 255 and decrementing on every iteration
            output[row,column,layer] = value 
        value = value - 2
        
cv2.imshow('Original', input_image)
# crop the image to apply the bluring filter
cropedImage = output[n-20:, :, :]
mask = cv2.GaussianBlur(mask, (9,9), sigmaX=1, sigmaY=10, borderType = cv2.BORDER_DEFAULT)       
# remaning part of image
output = output[:n-20, : , :]
# adding both part into one image
feathredimg = np.concatenate((output, cropedImage), axis=0)
cv2.imshow("Freathred Image", feathredimg)
cv2.waitKey(0)
cv2.destroyAllWindows() 

I created the solution for the given problem. Have a look. Hope this can help! This code is working perfectly with the given example image. enter image description here

Arslan Ali
  • 11
  • 3