Created
July 25, 2021 05:20
-
-
Save CathyLou/0d110538dbee5a18ff8f5c095acac932 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from PIL import Image | |
| import numpy as np | |
| from add_alpha_channel import add_alpha_channel | |
| import cv2 | |
| def color_transfer(source_img, target_img, image_mask=None, target_image_mask=None, img_filename=None): | |
| """ | |
| transfer iamge color according to target_image | |
| :param source_image: PIL image, RGB | |
| :param target_image: PIL image, RGB | |
| :param image_mask: mask, a numpy array, HxW, 0 or 255, uint8 | |
| :param target_image_mask: a numpy array, HxW, 0 or 255, uint8 | |
| :return image_transfered: color transfered image, a numpy array, RGB | |
| """ | |
| # if no mask, use whole image | |
| if type(image_mask) != np.ndarray: | |
| image_mask = np.ones((source_img.shape[0], source_img.shape[1])).astype(np.uint8) * 255 | |
| if type(target_image_mask) != np.ndarray: | |
| target_image_mask = np.ones((target_img.shape[0], target_img.shape[1])).astype(np.uint8) * 255 | |
| source_img = np.array(source_img).astype(np.uint8) | |
| target_img = np.array(target_img).astype(np.uint8) | |
| if isinstance(img_filename, str) and img_filename.find('黑') == -1: | |
| target_img = LAB_L_dec(target_img, scale=0.9) | |
| target_img[:, :] = cv2.meanStdDev(target_img)[0].reshape(1, 3) | |
| target_color_hls = cv2.cvtColor(target_img, cv2.COLOR_RGB2HLS_FULL) | |
| source_color_hls = cv2.cvtColor(source_img, cv2.COLOR_RGB2HLS_FULL) | |
| target_mean, target_std = cv2.meanStdDev(target_color_hls, mask=target_image_mask) | |
| source_mean, source_std = cv2.meanStdDev(source_color_hls, mask=image_mask) | |
| if target_mean[1] > 230: | |
| target_mean[1] *= 0.9 | |
| if target_mean[1] < 20: | |
| target_mean[1] += 10 | |
| img = source_img.copy() | |
| img = cv2.cvtColor(img, cv2.COLOR_RGB2HLS_FULL) | |
| out_img = img.copy().astype(np.float32) | |
| # color transfer in HLS color space | |
| out_img[:, :, 0] = target_mean[0] | |
| out_img[:, :, 2] = target_mean[2] | |
| out_img[:, :, 1] = out_img[:, :, 1] - source_mean[1] + target_mean[1] | |
| out_img = np.clip(out_img, 1, 254) | |
| out_img = out_img.astype(np.uint8) | |
| out_img = cv2.cvtColor(out_img, cv2.COLOR_HLS2RGB_FULL) | |
| image_mask = np.stack((image_mask, image_mask, image_mask), axis=2).astype(np.bool) | |
| out_img = out_img * image_mask + source_img * (~image_mask) | |
| processed_img = Image.fromarray(out_img) | |
| return processed_img | |
| def change_skin_color_pipelines(source_img,source_parsing,target_img,target_parsing): | |
| source_mask_array=np.array(source_parsing) | |
| for i in range(len(skin)): | |
| source_mask_array[source_mask_array==skin[i]]=255 | |
| source_mask_array[source_mask_array!=255]=0 | |
| # skin_mask=Image.fromarray(source_mask_array) | |
| face_mask_array=np.array(target_parsing) | |
| face_mask_array[face_mask_array==face]=255 | |
| face_mask_array[face_mask_array!=255]=0 | |
| result_img=color_transfer(source_img,target_img,source_mask_array,face_mask_array) | |
| return result_img |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment