imageFilter.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:supic 作者: Hirico 项目源码 文件源码
def lens_blur(input_path, depthmap_path, min_focal, max_focal, transition, radius, brightness, output_dir, speed=1):
    """ lens blur """
    im = Image.open(input_path)
    im.load()
    format = im.format
    depthmap = Image.open(depthmap_path)
    depth_px = depthmap.load()

    power = 10 ** brightness
    speed = int(min(speed, im.width, im.height))

    # prepare gradient filters and filtered images
    gradient_filters = []
    filtered_images = []
    copy_box = (0, 0, im.width, im.height)

    for i in range(radius):
        gradient_filters.append(ImageFilter.GaussianBlur(1))
        image_i = im.crop(copy_box)
        for j in range(i):
            image_i = image_i.filter(gradient_filters[i])

        filtered_images.append(image_i)

    # manipulate pixel
    for i in range(0, im.width, speed):
        for j in range(0, im.height, speed):
            depth = depth_px[i,j][0]
            box = (i, j, i + speed, j + speed)
            pixel = im.crop(box)
            if depth - max_focal >= transition or min_focal - depth >= transition:
                pixel = filtered_images[radius - 1].crop(box)
            elif depth - max_focal > 0:
                pixel = filtered_images[int((depth - max_focal)/transition*radius) - 1].crop(box)
            elif min_focal - depth > 0:
                pixel = filtered_images[int((min_focal - depth)/transition*radius) - 1].crop(box)
            im.paste(pixel, box)

    # output image
    enhancer = ImageEnhance.Brightness(im)
    im = enhancer.enhance(power)
    name = hex(int(time.time() * 100000))[2:]
    path = output_dir + '/' + str(name) + '.' + format
    im.save(path)
    return path
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号