Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion. Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules. |
|
|
Thread Tools | Search this Thread | Display Modes |
27th November 2022, 06:41 | #1 | Link |
Registered User
Join Date: Sep 2018
Posts: 391
|
Convert video node frame to numpy array or pillow image object (python)
I was wondering if someone could point me in the right direction. I've been struggling with it a portion of the day.
Currently I'm only able to output greyscale images from the frames. I was attempting to use code like this Code:
rgb_clip = vs.core.ffms2.Source(video.mkv) #1frame clip #number of planes , for rgb should be 3 anyway planes = rgb_clip.format.num_planes #making numpy array from vapoursynths videonode list_of_arrays = [np.array(rgb_clip.get_frame(0).get_read_array(i), copy=False) for i in range(planes)] numpy_array = np.dstack(list_of_arrays) Last edited by jlw_4049; 27th November 2022 at 06:53. |
27th November 2022, 08:42 | #2 | Link |
Registered User
Join Date: May 2011
Posts: 321
|
Not sure what you aim for. Video should be in rgb, maybe your really is, to go frame by frame - vs frame to np array and back to vs frame:
Code:
import numpy as np #from PIL import Image #import cv2 def format_shuffles(n,f): npArray = np.dstack([np.asarray(f.get_read_array(p)) for p in range(3)]) #latest vs: f[p] instead of f.get_read_array(p) #work here with npArray image #hsv = cv2.cvtColor(npArray, cv2.COLOR_BGR2HSV) #PIL_img = Image.fromarray(npArray, 'RGB') #or work with PIL image than convert it back to np array #npArray = np.array(PIL_img) f_out = f.copy() [np.copyto(np.asarray(f_out.get_write_array(p)), npArray[:, :, p]) for p in range(3)] return f_out clip = core.std.BlankClip(format=vs.YUV420P8) #here loading YUV video with whatever source plugin clip = clip.resize.Point(format=vs.RGB24, matrix_in_s = '709') clip = core.std.ModifyFrame(clip, clip, format_shuffles) clip.set_output(0) Last edited by _Al_; 27th November 2022 at 08:52. |
27th November 2022, 19:10 | #4 | Link |
Registered User
Join Date: May 2011
Posts: 321
|
You can just use view.py ,
just design your script in some python editor and just run the script Code:
import vapoursynth as vs clip = vs.core.lsmas.LibavSMASHSource('source.mp4') clip = ... some filters .... clip.set_output() if __name__ == "__main__": import view view.Preview(clip) Last edited by _Al_; 27th November 2022 at 19:23. |
29th November 2022, 15:57 | #5 | Link |
Registered User
Join Date: Sep 2018
Posts: 391
|
I actually played around with view.py for a bit. I did get it to work by editing the code, but it does not work as it stands. So I figured there was no support for it anymore.
I see now that you are the developer haha! So, I still haven't had a chance to get to my computer thanks to work, but I plan to try to play around with the code tonight/tomorrow after I'm off. I'll respond back here the results, also I'll look at view.py again! |
29th November 2022, 16:08 | #6 | Link |
Registered User
Join Date: May 2011
Posts: 321
|
Yes, it was for API3, Vapoursynth version R55 (not sure 100%) and below, not for API4, I thought because it is run in Python, fix would be easy for anyone, because it shows errors. Not using it, I use other versions which are not published. I'll look into it.
|
30th November 2022, 23:26 | #7 | Link | |
Registered User
Join Date: Jul 2021
Posts: 26
|
Quote:
https://github.com/quietvoid/vspreview-rs |
|
1st December 2022, 01:37 | #8 | Link | |
Registered User
Join Date: Sep 2018
Posts: 391
|
Quote:
Code:
import numpy as np #from PIL import Image #import cv2 def format_shuffles(n,f): npArray = np.dstack([np.asarray(f[p]) for p in range(3)]) #latest vs: f[p] instead of f.get_read_array(p) print(npArray) #work here with npArray image #hsv = cv2.cvtColor(npArray, cv2.COLOR_BGR2HSV) #PIL_img = Image.fromarray(npArray, 'RGB') #or work with PIL image than convert it back to np array #npArray = np.array(PIL_img) f_out = f.copy() [np.copyto(np.asarray(f_out.get_write_array(p)), npArray[:, :, p]) for p in range(3)] return f_out clip = core.lsmas.LWLibavSource(r"C:\Users\jlw_4\Desktop\rugrats test\The Rugrats Movie (1998).mkv") # clip = core.std.BlankClip(format=vs.YUV420P8) #here loading YUV video with whatever source plugin clip = clip.resize.Point(format=vs.RGB24, matrix_in_s = '709') clip = core.std.ModifyFrame(clip, clip, format_shuffles) format_shuffles(0, clip) clip.set_output(0) |
|
1st December 2022, 04:51 | #9 | Link |
Registered User
Join Date: May 2011
Posts: 321
|
should be:
format_shuffles(0, clip.get_frame(0)) simplest basic opencv player, it will play without delay frame by frame: Code:
import cv2 import numpy as np clip = core.lsmas.LWLibavSource(r"C:\Users\jlw_4\Desktop\rugrats test\The Rugrats Movie (1998).mkv") #assuming clip is loaded correctly from mkv rgb = core.resize.Point(clip, format=vs.RGB24, matrix_in_s = '709') for f in rgb.frames(): img = np.dstack([np.asarray(f.get_read_array(p)) for p in [2,1,0]]) #or f[p] ... cv2.imshow('movie', img) cv2.waitKey(1) Last edited by _Al_; 1st December 2022 at 04:54. |
1st December 2022, 16:52 | #10 | Link | ||
Registered User
Join Date: Sep 2018
Posts: 391
|
Quote:
Quote:
|
||
|
|