Last active
May 30, 2022 07:16
-
-
Save Tydus/987239fea966438d8a873fbb083240d6 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
well, i Copied the Thing and i get that error:
Traceback (most recent call last):
File "F:/Python Shit/Waifu2x in Python/i think that is Waifu2x in Python.py", line 4, in
infile, outfile, modelpath = sys.argv[1:]
ValueError: not enough values to unpack (expected 3, got 0)
edit: i did not have an image to convert btw.