Skip to content

Instantly share code, notes, and snippets.

@Tydus
Last active May 30, 2022 07:16
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Tydus/987239fea966438d8a873fbb083240d6 to your computer and use it in GitHub Desktop.
Save Tydus/987239fea966438d8a873fbb083240d6 to your computer and use it in GitHub Desktop.
waifu2x in 15 lines of Python by @marcan42
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)
@NekoTony
Copy link

NekoTony commented Sep 1, 2017

Could you possible link me to some models for this script?

@ELHoussineT
Copy link

where can we find the model?

@ELHoussineT
Copy link

ELHoussineT commented Sep 16, 2017

@Tydus
error:

im[:,:,-1] = np.clip(planes[0], 0, 1) * 255
ValueError: could not broadcast input array from shape (51,43) into shape (52,44)

Note that my input image is 51x43

@ELHoussineT
Copy link

ELHoussineT commented Sep 16, 2017

@NekoTony
in the main waifu repo
you can find models in models/photo/

@TungND-NTQ
Copy link

It's mean ./upconv_7/photo. Is this your model?

@Chaosminecraft
Copy link

Chaosminecraft commented Nov 18, 2020

@Tydus
error:

im[:,:,-1] = np.clip(planes[0], 0, 1) * 255
ValueError: could not broadcast input array from shape (51,43) into shape (52,44)

Note that my input image is 51x43

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment