LStation
4th January 2025, 18:23
I'd like assistance in replicating what a python script does for images, but now for video
# dct-r1.py based on github project
# https://github.com/joaocarvalhoopen/Detecting_the_original_resolution_of_an_upscale_image_DCT/blob/master/original_resolution_of_upscalled_image.ipynb
# using python 3.12 today
# pip3 install numpy
# pip3 install PIL
# pip3 install scipy
import os, sys
import numpy as np
from PIL import Image
from scipy.fftpack import dct
def process_image(image_path):
im = Image.open(image_path)
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
im_array = np.array(im)
gray_1 = rgb2gray(im_array)
im_gray = np.array(gray_1).astype(float)
t1 = dct(dct(im_gray, axis=0, norm='ortho'), axis=1, norm='ortho')
dct_img = Image.fromarray(t1.astype(np.uint8)).convert("L")
directory, filename = os.path.split(image_path)
new_filename = "dct_" + filename
dct_img.save(os.path.join(directory, new_filename))
for image_path in sys.argv[1:]:
process_image(image_path)
# dct.cmd ; drag n drop image(s) onto this to run the python script on them
# @echo off
# start /min cmd /c for %%i in (%*) do ( python dct-r1.py %%i )
here's a clip(video, google drive) (https://drive.google.com/file/d/1meB6ayC7nuidSDeHLZupkbLEF9IDakT2/view?usp=drive_link), and a still taken from it and processed(image, google drive) (https://drive.google.com/file/d/1yAYcN94RwGbhVVs4qGS9iGhcNZz-m8Cc/view?usp=drive_link), the black lines present in the noise give an impression on what the original resolution of the image was before being scaled ( it is 960x540 but appears it was scaled by 2 ).
I'm new to using Vapoursynth and I've got some basics of it working, commented out the lines that don't do anything now but hopefully convey the idea, in the end having grayscale_process() return dct_img
# dct-r1.vpy with ffms2 and fmtc plugins from vsdb.top
import vapoursynth as vs
#import numpy as np
#from scipy.fftpack import dct
#from PIL import Image
def grayscale_process(clip):
clip = clip.fmtc.matrix(mat="601")
clip = clip.std.ShufflePlanes(planes=[0], colorfamily=vs.GRAY)
# im_gray = np.array(clip).astype(float)
# t1 = dct(dct(im_gray, axis=0, norm='ortho'), axis=1, norm='ortho')
# dct_img = Image.fromarray(t1.astype(np.uint8)).convert("L")
return clip
core = vs.core
clip = core.ffms2.Source(source='P:/ATH.mp4')
grayscale_clip = grayscale_process(clip)
grayscale_clip.set_output()
# dct-r1.py based on github project
# https://github.com/joaocarvalhoopen/Detecting_the_original_resolution_of_an_upscale_image_DCT/blob/master/original_resolution_of_upscalled_image.ipynb
# using python 3.12 today
# pip3 install numpy
# pip3 install PIL
# pip3 install scipy
import os, sys
import numpy as np
from PIL import Image
from scipy.fftpack import dct
def process_image(image_path):
im = Image.open(image_path)
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
im_array = np.array(im)
gray_1 = rgb2gray(im_array)
im_gray = np.array(gray_1).astype(float)
t1 = dct(dct(im_gray, axis=0, norm='ortho'), axis=1, norm='ortho')
dct_img = Image.fromarray(t1.astype(np.uint8)).convert("L")
directory, filename = os.path.split(image_path)
new_filename = "dct_" + filename
dct_img.save(os.path.join(directory, new_filename))
for image_path in sys.argv[1:]:
process_image(image_path)
# dct.cmd ; drag n drop image(s) onto this to run the python script on them
# @echo off
# start /min cmd /c for %%i in (%*) do ( python dct-r1.py %%i )
here's a clip(video, google drive) (https://drive.google.com/file/d/1meB6ayC7nuidSDeHLZupkbLEF9IDakT2/view?usp=drive_link), and a still taken from it and processed(image, google drive) (https://drive.google.com/file/d/1yAYcN94RwGbhVVs4qGS9iGhcNZz-m8Cc/view?usp=drive_link), the black lines present in the noise give an impression on what the original resolution of the image was before being scaled ( it is 960x540 but appears it was scaled by 2 ).
I'm new to using Vapoursynth and I've got some basics of it working, commented out the lines that don't do anything now but hopefully convey the idea, in the end having grayscale_process() return dct_img
# dct-r1.vpy with ffms2 and fmtc plugins from vsdb.top
import vapoursynth as vs
#import numpy as np
#from scipy.fftpack import dct
#from PIL import Image
def grayscale_process(clip):
clip = clip.fmtc.matrix(mat="601")
clip = clip.std.ShufflePlanes(planes=[0], colorfamily=vs.GRAY)
# im_gray = np.array(clip).astype(float)
# t1 = dct(dct(im_gray, axis=0, norm='ortho'), axis=1, norm='ortho')
# dct_img = Image.fromarray(t1.astype(np.uint8)).convert("L")
return clip
core = vs.core
clip = core.ffms2.Source(source='P:/ATH.mp4')
grayscale_clip = grayscale_process(clip)
grayscale_clip.set_output()