1 Star 0 Fork 0

NeoWoodley/Neural-Style-Transfer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mask_transfer.py 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
Somshubra Majumdar 提交于 2017-06-27 11:57 . Better support for python 2
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import argparse
import os
import numpy as np
from scipy.misc import imread, imresize, imsave
# util function to load masks
def load_mask(mask_path, shape):
mask = imread(mask_path, mode="L") # Grayscale mask load
width, height, _ = shape
mask = imresize(mask, (width, height), interp='bicubic').astype('float32')
# Perform binarization of mask
mask[mask <= 127] = 0
mask[mask > 128] = 255
max = np.amax(mask)
mask /= max
return mask
# util function to apply mask to generated image
def mask_content(content, generated, mask):
width, height, channels = generated.shape
for i in range(width):
for j in range(height):
if mask[i, j] == 0.:
generated[i, j, :] = content[i, j, :]
return generated
parser = argparse.ArgumentParser(description='Neural style transfer color preservation.')
parser.add_argument('content_image', type=str, help='Path to content image')
parser.add_argument('generated_image', type=str, help='Path to generated image')
parser.add_argument('content_mask', type=str, help='Path to content mask')
args = parser.parse_args()
image_path = os.path.splitext(args.generated_image)[0] + "_masked.png"
generated_image = imread(args.generated_image, mode="RGB")
img_width, img_height, channels = generated_image.shape
content_image = imread(args.content_image, mode='RGB')
content_image = imresize(content_image, (img_width, img_height), interp='bicubic')
mask = load_mask(args.content_mask, generated_image.shape)
img = mask_content(content_image, generated_image, mask)
imsave(image_path, img)
print("Image saved at path : %s" % image_path)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neowoodley/Neural-Style-Transfer.git
git@gitee.com:neowoodley/Neural-Style-Transfer.git
neowoodley
Neural-Style-Transfer
Neural-Style-Transfer
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385