dariush-bahrami commited on
Commit
57e3eaa
1 Parent(s): d6f0737

Add colortransfer package

Browse files
colortransfer/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ from .transfer import transfer_color
2
+ from .utils import convert_bytes_to_pil
colortransfer/transfer.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2 as cv
3
+
4
+
5
+ def transfer_color(source_image: np.ndarray, target_image: np.ndarray) -> np.ndarray:
6
+ """Color transfer between images
7
+
8
+ Args:
9
+ source_image (np.ndarray): Color source image
10
+ target_image (np.ndarray): Target image
11
+
12
+ Returns:
13
+ np.ndarray: The result of the color transfer
14
+
15
+ Reference:
16
+ doi: 10.1109/38.946629
17
+ """
18
+ # RGB -> L*a*b*
19
+ src_img = cv.cvtColor(source_image, cv.COLOR_RGB2Lab)
20
+ dst_img = cv.cvtColor(target_image, cv.COLOR_RGB2Lab)
21
+
22
+ # Calculate mean and std
23
+ src_means, src_stds = src_img.mean(axis=(0, 1)), src_img.std(axis=(0, 1))
24
+ dst_means, dst_stds = dst_img.mean(axis=(0, 1)), dst_img.std(axis=(0, 1))
25
+
26
+ # Transfer
27
+ dst_img = dst_img - dst_means.reshape((1, 1, 3))
28
+ dst_img *= (dst_stds / src_stds).reshape((1, 1, 3))
29
+ dst_img += src_means.reshape((1, 1, 3))
30
+
31
+ # L*a*b* -> RGB
32
+ dst_img = np.clip(dst_img, 0, 255).astype(np.uint8)
33
+ dst_img = cv.cvtColor(dst_img, cv.COLOR_LAB2RGB)
34
+ return dst_img
colortransfer/utils.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
+ from PIL import Image
3
+
4
+
5
+ def convert_bytes_to_pil(bytes_string: bytes) -> Image:
6
+ buffer = BytesIO(bytes_string)
7
+ buffer.seek(0)
8
+ return Image.open(buffer)