1 Star 0 Fork 0

jiaxiaoyu/stroke-controllable-fast-style-transfer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
utils.py 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
LouieYang 提交于 2018-03-26 11:54 . fixed some old bugs
from PIL import Image
import numpy as np
import os
import functools
def load_image(path, shape=None, crop='center'):
img = Image.open(path).convert("RGB")
if isinstance(shape, (list, tuple)):
# crop to obtain identical aspect ratio to shape
width, height = img.size
target_width, target_height = shape[0], shape[1]
aspect_ratio = width / float(height)
target_aspect = target_width / float(target_height)
if aspect_ratio > target_aspect: # if wider than wanted, crop the width
new_width = int(height * target_aspect)
if crop == 'right':
img = img.crop((width - new_width, 0, width, height))
elif crop == 'left':
img = img.crop((0, 0, new_width, height))
else:
img = img.crop(((width - new_width) / 2, 0, (width + new_width) / 2, height))
else: # else crop the height
new_height = int(width / target_aspect)
if crop == 'top':
img = img.crop((0, 0, width, new_height))
elif crop == 'bottom':
img = img.crop((0, height - new_height, width, height))
else:
img = img.crop((0, (height - new_height) / 2, width, (height + new_height) / 2))
# resize to target now that we have the correct aspect ratio
img = img.resize((target_width, target_height))
elif isinstance(shape, (int, float)):
width, height = img.size
large = max(width, height)
ratio = shape / float(large)
width_n, height_n = ratio * width, ratio * height
img = img.resize((int(width_n), int(height_n)))
return img
def save_image(path, image):
res = Image.fromarray(np.uint8(np.clip(image, 0, 255.0)))
res.save(path)
def mkdir_if_not_exists(*args):
for arg in args:
if not os.path.exists(arg):
os.makedirs(arg)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jxyjxy/stroke-controllable-fast-style-transfer.git
git@gitee.com:jxyjxy/stroke-controllable-fast-style-transfer.git
jxyjxy
stroke-controllable-fast-style-transfer
stroke-controllable-fast-style-transfer
master

搜索帮助