File size: 803 Bytes
346b70f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
from PIL import Image
import cv2

DEFAULT_BILATERAL_D = 9
DEFAULT_BILATERAL_SIGMA_COLOR = 75
DEFAULT_BILATERAL_SIGMA_SPACE = 75


def simplify_texture(image, enabled=True, d=None, sigma_color=None, sigma_space=None):
    if not enabled:
        return image

    if d is None:
        d = DEFAULT_BILATERAL_D
    if sigma_color is None:
        sigma_color = DEFAULT_BILATERAL_SIGMA_COLOR
    if sigma_space is None:
        sigma_space = DEFAULT_BILATERAL_SIGMA_SPACE

    if hasattr(image, "convert"):
        rgb_image = image.convert("RGB")
        img_array = np.array(rgb_image, dtype=np.uint8)
    else:
        img_array = np.array(image, dtype=np.uint8)

    filtered = cv2.bilateralFilter(img_array, d, sigma_color, sigma_space)

    return Image.fromarray(filtered, "RGB")