-
-
Save yetone/187bab7f9679b7cf6cb6f8c1fec1586b to your computer and use it in GitHub Desktop.
waifu2x in 15 lines of Python by @marcan42
This file contains 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
import json, sys, numpy as np | |
from scipy import misc, signal | |
from PIL import Image | |
infile, outfile, modelpath = sys.argv[1:] | |
model = json.load(open(modelpath)) | |
im = Image.open(infile).convert("YCbCr") | |
im = misc.fromimage(im.resize((2*im.size[0], 2*im.size[1]), resample=Image.NEAREST)).astype("float32") | |
planes = [np.pad(im[:,:,0], len(model), "edge") / 255.0] | |
for step in model: | |
o_planes = [sum([signal.convolve2d(ip, np.float32(kernel), "valid") | |
for ip, kernel in zip(planes, weights)]) + np.float32(bias) | |
for bias, weights in zip(step["bias"], step["weight"])] | |
planes = [np.maximum(p, 0) + 0.1 * np.minimum(p, 0) for p in o_planes] | |
im[:,:,0] = np.clip(planes[0], 0, 1) * 255 | |
misc.toimage(im, mode="YCbCr").convert("RGB").save(outfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment