From 30eb89025679acff4f0ecd8eabec8bf6f7483393 Mon Sep 17 00:00:00 2001 From: NovaSagittarii Date: Sun, 15 Jan 2023 12:43:06 -0700 Subject: [PATCH] web prototype & green removal --- .gitmodules | 3 ++ process.py | 136 +++++++++++++++++++++++++++++++++++++++++++++++++ removegreen.py | 69 +++++++++++++++++++++++++ rumble-web | 1 + 4 files changed, 209 insertions(+) create mode 100644 .gitmodules create mode 100644 process.py create mode 100644 removegreen.py create mode 160000 rumble-web diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..ec04e07 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "rumble-web"] + path = rumble-web + url = https://github.com/NovaSagittarii/rumble-web diff --git a/process.py b/process.py new file mode 100644 index 0000000..2bc50dd --- /dev/null +++ b/process.py @@ -0,0 +1,136 @@ +import subprocess +import os +import sys + +import tkinter +from tkinter import ttk +from PIL import ImageTk, Image +# from tkinter import Image, ImageTk + +from removegreen import convert as processImage + +# print(processImage("./nate-doublepunch")) + +def vsum(a, b): + return tuple(a[i] + b[i] for i in range(len(a))) +def processSprites(folder): + vals = [] + files = os.listdir(folder) + filesOut = [] + for i, filename in enumerate(files): + if "out" in filename: continue + os.system(f"title {i} of {len(files)}") + path = os.path.join(folder, filename) + coords = processImage(path) + # x = -(coords['left'] + coords['right'])/2 + # y = -(coords['bottom'] + coords['top'])/2 + x,y = 0,0 + vals.append((x, y)) + filesOut.append(coords['file']); + delta = [vsum(vals[i], list(-x for x in vals[i-1 if i > 1 else i])) for i in range(len(vals))] + + # def savePosn(event): + # global lastx, lasty + # lastx, lasty = event.x, event.y + + # def addLine(event): + # canvas.create_line((lastx, lasty, event.x, event.y)) + # savePosn(event) + + global framecount + global pos + global fstart + global fend + + pos = (0, 0) + framecount = 0 + fstart = 0 + fend = 100 + def mousedown(event): + global pos + global framecount + framecount = event.x + # print(event.x) + pos = (0, 0) + def restart(): + global framecount + framecount = 0 + + print(filesOut[0]) + im = Image.open(filesOut[0]) + print(im.mode) + + global yumyum + yumyum = [] + + def nextframe(): + global pos + global framecount + global yumyum + global fstart + global fend + i = fstart+ ( (framecount) % (min(fend, len(delta))-fstart) ) + pos = vsum(pos, delta[i]) + dx, dy = 0,0 #vals[i] + + canvas.delete("all") + myimg = ImageTk.PhotoImage(file=filesOut[i]) + yumyum = myimg + # img=ImageTk.PhotoImage(Image.open("camels.jpg")) + # canvas.create_image(250, 250, anchor=CENTER, image=img) + + # photoimage = ImageTk.PhotoImage(file="example.png") + # canvas.create_image(150, 150, image=photoimage) + + # canvas.create_image(-pos[0]+dx, -pos[1]+dy, image=myimg, anchor='nw') + canvas.create_image(100, 100, image=myimg) # anchor='nw') + canvas.create_text(20, 20, text=f'{fstart}->{fend} {framecount} {i}', anchor='nw', font='TkMenuFont', fill='red') + + framecount += 1 + root.after(30, nextframe) + + root = tkinter.Tk() + # root.columnconfigure(0, weight=1) + # root.rowconfigure(0, weight=1) + # root.geometry("400x400") + + canvas = tkinter.Canvas(root, width=400, height=400, background='white') + # canvas.grid(column=0, row=0, sticky=(N, W, E, S)) + canvas.bind("", mousedown) + # canvas.bind("", savePosn) + # canvas.bind("", addLine) + canvas.pack() + + def setstart(): + global fstart + fstart = int(inputtxt.get(1.0, "end-1c")) + def setend(): + global fend + fend = int(inputtxt.get(1.0, "end-1c")) + + # TextBox Creation + inputtxt = tkinter.Text(root, height=1, width=20) + inputtxt.pack() + # Button Creation + printButton = tkinter.Button(root, text="start", command=setstart) + printButton.pack() + printButton = tkinter.Button(root, text="end", command=setend) + printButton.pack() + printButton = tkinter.Button(root, text="reset", command=restart) + printButton.pack() + + root.after(30, nextframe) + root.mainloop() + + return ("// END //",) + +def main(): + # Load image and convert it to RGBA, so it contains alpha channel + # print(sys.argv) + file_path = sys.argv[1] if len(sys.argv) >= 2 else "nate-fullanphufightsource" + print(processSprites(file_path)) + +if __name__ == '__main__': + main() + +# https://stackoverflow.com/questions/2810970/how-to-remove-a-green-screen-portrait-background \ No newline at end of file diff --git a/removegreen.py b/removegreen.py new file mode 100644 index 0000000..06db4ae --- /dev/null +++ b/removegreen.py @@ -0,0 +1,69 @@ +# https://github.com/kimmobrunfeldt/howto-everything/blob/master/remove-green.md + +""" +Removes greenscreen from an image. +Usage: python greenscreen_remove.py image.jpg +""" + +from PIL import Image +import sys +import os + +def rgb_to_hsv(r, g, b): + maxc = max(r, g, b) + minc = min(r, g, b) + v = maxc + if minc == maxc: + return 0.0, 0.0, v + s = (maxc-minc) / maxc + rc = (maxc-r) / (maxc-minc) + gc = (maxc-g) / (maxc-minc) + bc = (maxc-b) / (maxc-minc) + if r == maxc: + h = bc-gc + elif g == maxc: + h = 2.0+rc-bc + else: + h = 4.0+gc-rc + h = (h/6.0) % 1.0 + return h, s, v + +GREEN_RANGE_MIN_HSV = (100, 60, 70) # (100, 80, 70) +GREEN_RANGE_MAX_HSV = (185, 255, 255) + +def convert(file_path): + name, ext = os.path.splitext(file_path) + im = Image.open(file_path) + im = im.convert('RGBA') + + # Go through all pixels and turn each 'green' pixel to transparent + + output = {} + newfile = name + '-out.png' + output['file'] = newfile + skip = True + + if not skip: + pix = im.load() + width, height = im.size + for x in range(width): + for y in range(height): + r, g, b, a = pix[x, y] + h_ratio, s_ratio, v_ratio = rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0) + h, s, v = (h_ratio * 360, s_ratio * 255, v_ratio * 255) + + min_h, min_s, min_v = GREEN_RANGE_MIN_HSV + max_h, max_s, max_v = GREEN_RANGE_MAX_HSV + if min_h <= h <= max_h and min_s <= s <= max_s and min_v <= v <= max_v: + pix[x, y] = (0, 0, 0, 0) + im.save(newfile) + return output +def main(): + # Load image and convert it to RGBA, so it contains alpha channel + # print(sys.argv) + file_path = sys.argv[1] + convert(file_path) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/rumble-web b/rumble-web new file mode 160000 index 0000000..ac75b49 --- /dev/null +++ b/rumble-web @@ -0,0 +1 @@ +Subproject commit ac75b4955e8f5f373c62053896e96499222be692