1 |
""" |
2 |
@author: John NGUI |
3 |
""" |
4 |
|
5 |
from esys.pyvisi.constant import ImageFormat |
6 |
from os import system |
7 |
|
8 |
class Movie: |
9 |
""" |
10 |
Class that creates a file called 'make_movie' by default (if a parameter |
11 |
file name is not speficied) which contains a list of parameters required |
12 |
by the 'ppmtompeg' command to generate a movie from a series of images. |
13 |
""" |
14 |
|
15 |
def __init__(self, parameter_file = "make_movie"): |
16 |
""" |
17 |
Initialise the generation of the movie. |
18 |
|
19 |
@type parameter_file: String |
20 |
@param parameter_file: Name of the file containing the list of |
21 |
parameters required by the 'ppmtompeg' command. |
22 |
""" |
23 |
|
24 |
self.__parameter_file = parameter_file |
25 |
|
26 |
def makeMovie(self, input_directory, first_image, last_image, movie): |
27 |
""" |
28 |
Coordinate the generation of the movie. |
29 |
|
30 |
@type input_directory: String |
31 |
@param input_directory: Directory in which the series of images can |
32 |
be found |
33 |
@type first_image: String |
34 |
@param first_image: First image name (including the extension) |
35 |
@type last_image: String |
36 |
@param last_image: Last image name (including the extension) |
37 |
@type movie : String |
38 |
@param movie: Movie name (including the extension) |
39 |
""" |
40 |
|
41 |
self.__input_directory = input_directory |
42 |
self.__first_image = first_image |
43 |
self.__last_image = last_image |
44 |
self.__movie = movie |
45 |
|
46 |
self.__splitInput() |
47 |
self.__retrieveFirstImageDetails() |
48 |
self.__retrieveLastImageDetails() |
49 |
self.__retrieveConversionCommand() |
50 |
self.__generateParameterFile() |
51 |
|
52 |
# If a parameter file name was not specified, then the default file |
53 |
# will be deleted automatically once the movie has been generated. |
54 |
# However, if a paramter file name was specified, the file will be |
55 |
# maintained. |
56 |
system('ppmtompeg ' + self.__parameter_file) |
57 |
if(self.__parameter_file == "make_movie"): |
58 |
system('rm ' + self.__parameter_file) |
59 |
|
60 |
def __splitInput(self): |
61 |
""" |
62 |
Split the image label (i.e. temp-0001.jpg) to separate the |
63 |
image name (i.e. temp-0001) and image format (i.e. jpg). |
64 |
""" |
65 |
|
66 |
# Image format (i.e. jpg) |
67 |
self.__image_format = self.__first_image.split('.')[1] |
68 |
# First image name. |
69 |
self.__first_image = self.__first_image.split('.')[0] |
70 |
# Last image name. |
71 |
self.__last_image = self.__last_image.split('.')[0] |
72 |
|
73 |
def __retrieveFirstImageDetails(self): |
74 |
""" |
75 |
Retrieve the first image name details. |
76 |
""" |
77 |
|
78 |
# For a image called 'temp-0001.jpg', self.__first_image_number |
79 |
# will be '0001', while self.__image_prefix will be 'temp-'. |
80 |
self.__first_image_number = "" |
81 |
self.__image_prefix = "" |
82 |
|
83 |
for i in range(len(self.__first_image)): |
84 |
if(self.__first_image[i].isdigit()): # Retrieve first image number. |
85 |
self.__first_image_number = \ |
86 |
self.__first_image_number + self.__first_image[i] |
87 |
else: # Retrieve image prefix. |
88 |
self.__image_prefix = \ |
89 |
self.__image_prefix + self.__first_image[i] |
90 |
|
91 |
def __retrieveLastImageDetails(self): |
92 |
""" |
93 |
Retrieve the last image name details. |
94 |
""" |
95 |
|
96 |
self.__last_image_number = "" |
97 |
|
98 |
for i in range(len(self.__last_image)): |
99 |
if(self.__last_image[i].isdigit()): # Retrieve last image number. |
100 |
self.__last_image_number = \ |
101 |
self.__last_image_number + self.__last_image[i] |
102 |
|
103 |
# The last image number is deducted by one. This allows the user |
104 |
# to use the same image range when generating a movie as the |
105 |
# ones used for the for-loop. |
106 |
#self.__last_image_number = unicode(int(self.__last_image_number) - 1) |
107 |
|
108 |
def __retrieveConversionCommand(self): |
109 |
""" |
110 |
Retrieve the conversion command (depending on the image format) |
111 |
required by the 'ppmtompeg' command. |
112 |
""" |
113 |
print "-----", self.__image_format |
114 |
if(self.__image_format.endswith(ImageFormat.JPG)): |
115 |
self.__command = 'jpeg' |
116 |
elif(self.__image_format.endswith(ImageFormat.BMP)): |
117 |
self.__command = ImageFormat.BMP |
118 |
elif(self.__image_format.endswith(ImageFormat.PNM)): |
119 |
self.__command = ImageFormat.PNM |
120 |
elif(self.__image_format.endswith(ImageFormat.PNG)): |
121 |
self.__command = ImageFormat.PNG |
122 |
elif(self.__image_format.endswith(ImageFormat.TIF)): |
123 |
self.__command = ImageFormat.TIF |
124 |
else: |
125 |
raise IOError("ERROR: Invalid image format.") |
126 |
|
127 |
|
128 |
def __generateParameterFile(self): |
129 |
""" |
130 |
Write the list of parameters into the file. |
131 |
""" |
132 |
|
133 |
parameter_file = open(self.__parameter_file, 'w') |
134 |
|
135 |
parameter_file.write('PATTERN IBBPBBPBBPBBPBBP\n' + |
136 |
'OUTPUT ' + self.__movie + '\n' |
137 |
'BASE_FILE_FORMAT PNM\n' + |
138 |
'INPUT_CONVERT ' + self.__command + 'topnm *\n' + |
139 |
'GOP_SIZE 16\n' + |
140 |
'SLICES_PER_FRAME 10\n' + |
141 |
'INPUT_DIR ' + self.__input_directory + '\n' + |
142 |
'INPUT\n' + |
143 |
self.__image_prefix + '*.' + self.__image_format + ' [' + \ |
144 |
self.__first_image_number + '-' + \ |
145 |
self.__last_image_number + ']\n' |
146 |
'END_INPUT\n' + |
147 |
'PIXEL HALF\n' + |
148 |
'RANGE 10\n' + |
149 |
'PSEARCH_ALG LOGARITHMIC\n' + |
150 |
'BSEARCH_ALG CROSS2\n' + |
151 |
'IQSCALE 8\n' + |
152 |
'PQSCALE 10\n' + |
153 |
'BQSCALE 25\n' + |
154 |
'REFERENCE_FRAME DECODED\n' + |
155 |
'FORCE_ENCODE_LAST_FRAME\n' + |
156 |
'ASPECT_RATIO 1\n' + |
157 |
'FRAME_RATE 24') |
158 |
|
159 |
parameter_file.close() |
160 |
|