简体   繁体   中英

Lens Blur effect using Python Pillow

In Pillow, we can blur image using filters:

blurred_image = original_image.filter(ImageFilter.BLUR)

But this is not a true lens effect. Is it possible using some custom filter? Any example?

(Number 2 is what I need. Number 3 is what pillow BLUR filter.)

在此处输入图像描述

In Pillow, we can blur image using filters:

blurred_image = original_image.filter(ImageFilter.BLUR)

But this is not a true lens effect. Is it possible using some custom filter? Any example?

(Number 2 is what I need. Number 3 is what pillow BLUR filter.)

在此处输入图片说明

In Pillow, we can blur image using filters:

blurred_image = original_image.filter(ImageFilter.BLUR)

But this is not a true lens effect. Is it possible using some custom filter? Any example?

(Number 2 is what I need. Number 3 is what pillow BLUR filter.)

在此处输入图片说明

I tried to convert above pseudo-code into real code but not getting desired result. any idea on where I am doing wrong ?

import cv2
import numpy as np
 
def gammaCorrection(src, gamma):
    invGamma = 1 / gamma 
    table = [((i / 255) ** invGamma) * 255 for i in range(256)]
    table = np.array(table, np.uint8) 
    return cv2.LUT(src, table)     
 
image = cv2.imread(r'C:\Users\test\Desktop\test.jpg')
gammaCorrectedImage = gammaCorrection(image, 3.0)   #Or whatever power works for you
ksize = (40, 40)
bokeh = cv2.blur(gammaCorrectedImage, ksize)        #discBlur(gammaCorrectedImage)
bokeh = gammaCorrection(bokeh, 0.33)                #To get back the original gamma.
blurImage = cv2.blur(image, ksize)                  #discBlur(image)
finalImage = cv2.max(bokeh, blurImage)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM