Skip to content

Instantly share code, notes, and snippets.

@AlphaAtlas
Last active November 29, 2019 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlphaAtlas/09f70dacd7f54475a56bba48a5589595 to your computer and use it in GitHub Desktop.
Save AlphaAtlas/09f70dacd7f54475a56bba48a5589595 to your computer and use it in GitHub Desktop.
Debug 1
import numpy as np
import functools
import vapoursynth as vs
import random
from vapoursynth import core
mscale = 1
pad = 32
#Makes a horizontal or vertical grayscale, FP32 gradient ramp VS clip
#White is on the right/bottom, black is on the left/top
#Based on https://forum.doom9.org/showthread.php?p=1885013
def make_gradient(WIDTH, HEIGHT, length, vertical = False):
#https://stackoverflow.com/questions/4337902/how-to-fill-opencv-image-with-one-solid-color
def create_blank(width, height, color=(0, 0, 0)):
image = np.zeros((height, width, 3), np.uint8)
#color = tuple(reversed(color))
image[:] = color
return image
#https://github.com/KotoriCANOE/MyTF/blob/master/utils/vshelper.py
def float32_vsclip(s, clip=None):
assert isinstance(s, np.ndarray)
core = vs.get_core()
if len(s.shape) <= 3: s = s.reshape([1] + list(s.shape))
num = s.shape[-4]
height = s.shape[-3]
width = s.shape[-2]
planes = 1
if clip is None:
clip = core.std.BlankClip(None, width, height, vs.GRAYS, num)
def convert_func(n, f):
fout = f.copy()
for p in range(planes):
d = np.array(fout.get_write_array(p), copy=False)
np.copyto(d, s[n, :, :, p])
del d
return fout
return core.std.ModifyFrame(clip, clip, convert_func)
color1 = (0)
color2 = (255)
img1 = create_blank(WIDTH, HEIGHT, color1).astype(np.float32)/255.0
img2 = create_blank(WIDTH, HEIGHT, color2).astype(np.float32)/255.0
if vertical:
c = np.linspace(0, 1, HEIGHT)[:, None, None]
else:
c = np.linspace(0, 1, WIDTH)[None,:, None]
gradient = img1 + (img2 - img1) * c
clip = float32_vsclip(gradient)
return core.std.Loop(clip, length)
#Like stackhorizontal, but with padding
def merge_horizontal(left, right):
left = core.resize.Spline36(left, format = vs.RGBS)
right = core.resize.Spline36(right, format = vs.RGBS)
lwidth = left.width - pad
rwidth = right.width - pad
if (left.height, left.num_frames, left.format) != (right.height, right.num_frames, right.format):
raise Exception("Left and right clip must have the same height, format and number of frames!")
mask = make_gradient(pad * 2 * mscale, left.height, left.num_frames)
mask = core.std.AddBorders(mask, left = lwidth - pad * mscale, color = [0.0])
mask = core.std.AddBorders(mask, right = rwidth - pad * mscale, color = [1.0])
#mask = core.resize.Spline36(mask, format = vs.GRAY8)
#raise Exception(lwidth, rwidth)
left = core.std.AddBorders(left, right = rwidth - pad * mscale)
right = core.std.AddBorders(right, left = lwidth - pad * mscale)
#raise Exception(left.width, left.height, right.width, right.height, mask.width, mask.height, mask.format)
core.std.StackVertical([left, right, core.resize.Spline36(mask, format = vs.RGBS), core.std.MaskedMerge(left, right, mask)]).set_output()
return core.std.MaskedMerge(left, right, mask)
#Like stackvertical, but with padding
def merge_vertical(top, bottom):
if (top.width, top.num_frames, top.format) != (bottom.width, bottom.num_frames, bottom.format):
raise Exception("Top and bottom clip must have the same height, format and number of frames!")
mask = make_gradient(top.width, pad * mscale, top.num_frames, vertical = True)
mask = core.std.AddBorders(mask, top = top.height - pad * mscale, color = [0.0])
mask = core.std.AddBorders(mask, bottom = bottom.height - pad * mscale, color = [1.0])
mask = core.resize.Spline36(mask, format = vs.GRAY8)
theight = top.height
bheight = bottom.height
top = core.std.AddBorders(top, bottom = bheight - pad * mscale)
bottom = core.std.AddBorders(bottom, top = theight - pad * mscale)
return core.std.Merge(top, bottom)
def chunk_clip(clip, pad):
#Split the image into 4 images, with some overlap over the seams
hcrop = clip.width/2 - pad
vcrop = clip.height/2 - pad
return [
core.std.Crop(clip, bottom = vcrop, right = hcrop),
core.std.Crop(clip, bottom = vcrop, left = hcrop),
core.std.Crop(clip, top = vcrop, right = hcrop),
core.std.Crop(clip, top = vcrop, left = hcrop)]
c = []
c.append(core.std.BlankClip(width = 200, height = 200, format = vs.RGBS, color = [random.random(), random.random(), random.random()]))
c.append(core.std.BlankClip(width = 200, height = 200, format = vs.RGBS, color = [random.random(), random.random(), random.random()]))
c.append(core.std.BlankClip(width = 200, height = 200, format = vs.RGBS, color = [random.random(), random.random(), random.random()]))
c.append(core.std.BlankClip(width = 200, height = 200, format = vs.RGBS, color = [random.random(), random.random(), random.random()]))
#clop = merge_horizontal(c[0], c[1], pad)
#clop2 = merge_horizontal(c[2], c[3], pad)
#clop = merge_vertical(clop, clop2, pad)
#clop = core.resize.Spline36(clop, format = vs.RGB24)
clip = core.std.StackHorizontal([c[0], c[1]])
clop = core.std.StackHorizontal([c[2], c[3]])
clop = core.std.StackVertical([clip, clop])
clop = core.resize.Spline36(clop, format = vs.RGB24)
results = chunk_clip(clop, pad)
tophalf = merge_horizontal(results[0], results[1])
bottomhalf = merge_horizontal(results[2], results[3])
#buffer = merge_vertical(tophalf, bottomhalf)
#tophalf.set_output()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment