-
Notifications
You must be signed in to change notification settings - Fork 5
/
nadir-patcher-slow.py
137 lines (111 loc) · 5.63 KB
/
nadir-patcher-slow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# Author: [email protected]
# Created: 2020-06-02
# Copyright: Trek View
# Licence: GNU AGPLv3
# -------------------------------------------------------------------------------
import subprocess
import shlex
import os
import shutil
import sys
import platform
#Global Variables for temp folder and files that will be automatically deleted when process complete.
temp_folder_name="nadir_patcher_temp"
temp_output1=""
temp_output2=""
temp_output3=""
temp_output4=""
temp_output5=""
def main(argv):
#Create nadir_patcher_temp folder
if not os.path.exists(temp_folder_name):
os.makedirs(temp_folder_name)
#get parameter values from command line arguments
main_file_dir = sys.argv[1]
overlay_file = sys.argv[2]
nadir_percentage = float(int(sys.argv[3])/100)
#Check operating system
if platform.system() =="Linux":
print("Linux")
os_path_set="/"
elif platform.system() =="Windows":
os_path_set="\\"
else:
os_path_set="/"
#Check input path is file or directory
check_file_dir=os.path.isdir(main_file_dir)
print(check_file_dir)
if check_file_dir:
#process one by one image or video from directory
print('staring...')
for r, d, f in os.walk(main_file_dir):
for file in f:
file_path = os.path.join(r, file)
main_process(file_path,overlay_file,nadir_percentage,os_path_set)
shutil.rmtree(temp_folder_name)
print('Done Successfully')
else:
print('staring...')
#Process image or video from file path
main_process(main_file_dir,overlay_file,nadir_percentage,os_path_set)
shutil.rmtree(temp_folder_name)
print('Done Successfully')
def main_process(main_file_dir,overlay_file,nadir_percentage,os_path_set):
input_file_name=(os.path.splitext(os.path.basename(main_file_dir))[0])
input_path_name, input_file_extension = os.path.splitext(main_file_dir)
nadir_file_name=(os.path.splitext(os.path.basename(overlay_file))[0])
nadir_path_name, nadir_file_extension = os.path.splitext(overlay_file)
output_file=input_file_name+"_"+nadir_file_name+"_"+str(sys.argv[3])+"pc"+input_file_extension
output_directory = sys.argv[4]
if not os.path.exists(output_directory):
os.makedirs(output_directory)
output_file=output_directory+os_path_set+output_file
temp_output1=temp_folder_name+os_path_set+"temp_result1"+nadir_file_extension
temp_output2=temp_folder_name+os_path_set+"temp_result2"+nadir_file_extension
temp_output3=temp_folder_name+os_path_set+"temp_result3"+nadir_file_extension
temp_output4=temp_folder_name+os_path_set+"temp_result4"+nadir_file_extension
temp_output5=temp_folder_name+os_path_set+"temp_result5"+nadir_file_extension
#Rotate nadir file 180 degrees and strip any metadata (https://imagemagick.org/script/command-line-options.php#rotate
# https://imagemagick.org/script/command-line-options.php#strip)
comd1="magick "+"\""+overlay_file+"\""+" -rotate 180 -strip {}".format(temp_output1)
subprocess.call(comd1,shell=True)
print('process 1 done')
#Turn nadir file into equirectangular projection (https://imagemagick.org/script/command-line-options.php#distort)
comd2="magick "+"\""+temp_output1+"\""+" -distort DePolar 0 "+temp_output2
subprocess.call(comd2,shell=True)
print('process 2 done')
#Flip nadir file vertically (https://imagemagick.org/script/command-line-options.php#flip)
comd3="magick "+"\""+temp_output2+"\""+" -flip "+temp_output3
subprocess.call(comd3,shell=True)
print('process 3 done')
#Flip nadir file horizontally (https://imagemagick.org/script/command-line-options.php#flop)
comd4="magick "+"\""+temp_output3+"\""+" -flop "+temp_output4
subprocess.call(comd4,shell=True)
print('process 4 done')
cmd = 'ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv="s=x:p=0"'
args = shlex.split(cmd)
args.append(main_file_dir)
#Calculate the nadir dimensions for overlay
ffprobe_output = subprocess.check_output(args).decode('utf-8')
wh_values = ffprobe_output.split("x")
width_str = wh_values[0]
height_float = float(wh_values[1])
overlay_height_temp=int(float(height_float*nadir_percentage))
overlay_position=str(int(height_float)-overlay_height_temp)
overlay_height= str(overlay_height_temp)
#Overlay the nadir and output the new file into specified directory
overlay_scale ="{}:{}".format(width_str,overlay_height)
comd5 ="ffmpeg -y -i {} -vf scale={} {}".format(temp_output4,overlay_scale,temp_output5)
subprocess.call(comd5,shell=True)
print('process 5 done')
if(main_file_dir.endswith(".MP4") or main_file_dir.endswith(".mp4")):
comd6="ffmpeg -y -i "+"\""+main_file_dir+"\""+" -i "+temp_output5+" -filter_complex \"[0:v][1:v]overlay=x=0:y=(main_h)-overlay_h\" "+"\""+output_file+"\""
subprocess.call(comd6,shell=True)
else:
comd6 ="magick "+"\""+main_file_dir+"\""+" "+temp_output5+" -geometry +0+"+overlay_position+" -composite "+"\""+output_file+"\""
subprocess.call(comd6,shell=True)
print('process 6 done')
if __name__ == '__main__':
main(sys.argv[1:])