1 |
|
2 |
######################################################## |
3 |
# |
4 |
# Copyright (c) 2003-2008 by University of Queensland |
5 |
# Earth Systems Science Computational Center (ESSCC) |
6 |
# http://www.uq.edu.au/esscc |
7 |
# |
8 |
# Primary Business: Queensland, Australia |
9 |
# Licensed under the Open Software License version 3.0 |
10 |
# http://www.opensource.org/licenses/osl-3.0.php |
11 |
# |
12 |
######################################################## |
13 |
|
14 |
__copyright__="""Copyright (c) 2003-2008 by University of Queensland |
15 |
Earth Systems Science Computational Center (ESSCC) |
16 |
http://www.uq.edu.au/esscc |
17 |
Primary Business: Queensland, Australia""" |
18 |
__license__="""Licensed under the Open Software License version 3.0 |
19 |
http://www.opensource.org/licenses/osl-3.0.php""" |
20 |
__url__="http://www.uq.edu.au/esscc/escript-finley" |
21 |
|
22 |
""" |
23 |
@var __author__: name of author |
24 |
@var __copyright__: copyrights |
25 |
@var __license__: licence agreement |
26 |
@var __url__: url entry point on documentation |
27 |
@var __version__: version |
28 |
@var __date__: date of the version |
29 |
""" |
30 |
|
31 |
__author__="John Ngui, john.ngui@uq.edu.au" |
32 |
|
33 |
|
34 |
from esys.pyvisi.constant import ImageFormat |
35 |
import os |
36 |
|
37 |
class Movie: |
38 |
""" |
39 |
Class that creates a file called 'make_movie' by default (if a parameter |
40 |
file name is not speficied) which contains a list of parameters required |
41 |
by the 'ppmtompeg' command to generate a movie from a series of images. |
42 |
|
43 |
@attention: A movie cannot be generated from postscript (.ps) images. |
44 |
""" |
45 |
|
46 |
def __init__(self, parameter_file = "make_movie"): |
47 |
""" |
48 |
Initialise the generation of the movie. If a parameter file name |
49 |
is supplied, the file will not be deleted once the movie has been |
50 |
generated. Otherwise, the temporary parameter file created will |
51 |
automatically be removed at the end. |
52 |
|
53 |
@type parameter_file: String |
54 |
@param parameter_file: Name of the file containing the list of |
55 |
parameters required by the 'ppmtompeg' command. |
56 |
""" |
57 |
|
58 |
self.__parameter_file = parameter_file |
59 |
return |
60 |
|
61 |
|
62 |
def imageRange(self, input_directory, first_image, last_image): |
63 |
""" |
64 |
The image range from which the movie is to be generated from. |
65 |
|
66 |
@type input_directory: String |
67 |
@param input_directory: Directory in which the series of images can |
68 |
be found |
69 |
@type first_image: String |
70 |
@param first_image: First image name (including the extension) |
71 |
@type last_image: String |
72 |
@param last_image: Last image name (including the extension) |
73 |
""" |
74 |
|
75 |
# Keeps track whether an image range or image list was provided as |
76 |
# the source for generating the movie. |
77 |
self.__image_range_used = True |
78 |
self.__input_directory = input_directory |
79 |
self.__first_image = first_image |
80 |
self.__last_image = last_image |
81 |
|
82 |
self.__splitInput() |
83 |
self.__retrieveFirstImageDetails() |
84 |
self.__retrieveLastImageDetails() |
85 |
self.__retrieveConversionCommand() |
86 |
return |
87 |
|
88 |
def imageList(self, input_directory, image_list): |
89 |
""" |
90 |
The image list from which the movie is to be generated from. |
91 |
|
92 |
@type input_directory: String |
93 |
@param input_directory: Directory in which the series of images can |
94 |
be found |
95 |
@type image_list: List |
96 |
@param image_list: List of images name (including the extension) |
97 |
""" |
98 |
|
99 |
self.__image_range_used = False |
100 |
self.__input_directory = input_directory |
101 |
self.__images = "" |
102 |
|
103 |
self.__first_image = image_list[0] # Get first image in the list. |
104 |
self.__splitInput() |
105 |
self.__retrieveConversionCommand() |
106 |
|
107 |
for i in image_list: |
108 |
self.__images += i + '\n' |
109 |
return |
110 |
|
111 |
def makeMovie(self, movie): |
112 |
""" |
113 |
Generate the movie. |
114 |
|
115 |
@type movie : String |
116 |
@param movie: Movie name (including the .mpg extension) |
117 |
""" |
118 |
|
119 |
self.__movie = movie |
120 |
self.__generateParameterFile() |
121 |
|
122 |
# If a parameter file name was not specified, then the default file |
123 |
# will be deleted automatically once the movie has been generated. |
124 |
# However, if a paramter file name was specified, the file will be |
125 |
# maintained. |
126 |
os.system('ppmtompeg ' + self.__parameter_file) |
127 |
if(self.__parameter_file == "make_movie"): |
128 |
os.system('rm ' + self.__parameter_file) |
129 |
|
130 |
return |
131 |
|
132 |
def __splitInput(self): |
133 |
""" |
134 |
Split the image label (i.e. temp-0001.jpg) to separate the |
135 |
image name (i.e. temp-0001) and image format (i.e. jpg). |
136 |
""" |
137 |
|
138 |
first_image_split = self.__first_image.split('.') |
139 |
# Image format (i.e. jpg) |
140 |
self.__image_format = first_image_split[len(first_image_split) - 1] |
141 |
# First image name. |
142 |
self.__first_image = \ |
143 |
self.__first_image.rstrip('.' + self.__image_format) |
144 |
|
145 |
if(self.__image_range_used == True): |
146 |
# Last image name. |
147 |
self.__last_image = \ |
148 |
self.__last_image.rstrip('.' + self.__image_format) |
149 |
return |
150 |
|
151 |
def __retrieveFirstImageDetails(self): |
152 |
""" |
153 |
Retrieve the first image name details. |
154 |
""" |
155 |
|
156 |
# For a image called 'temp-0001.jpg', self.__first_image_number |
157 |
# will be '0001', while self.__image_prefix will be 'temp-'. |
158 |
self.__first_image_number = "" |
159 |
self.__image_prefix = "" |
160 |
|
161 |
for i in range(len(self.__first_image)): |
162 |
if(self.__first_image[i].isdigit()): # Retrieve first image number. |
163 |
self.__first_image_number = \ |
164 |
self.__first_image_number + self.__first_image[i] |
165 |
else: # Retrieve image prefix. |
166 |
self.__image_prefix = \ |
167 |
self.__image_prefix + self.__first_image[i] |
168 |
return |
169 |
|
170 |
def __retrieveLastImageDetails(self): |
171 |
""" |
172 |
Retrieve the last image name details. |
173 |
""" |
174 |
|
175 |
self.__last_image_number = "" |
176 |
|
177 |
for i in range(len(self.__last_image)): |
178 |
if(self.__last_image[i].isdigit()): # Retrieve last image number. |
179 |
self.__last_image_number = \ |
180 |
self.__last_image_number + self.__last_image[i] |
181 |
|
182 |
return |
183 |
|
184 |
def __retrieveConversionCommand(self): |
185 |
""" |
186 |
Retrieve the conversion command (depending on the image format) |
187 |
required by the 'ppmtompeg' command. |
188 |
""" |
189 |
|
190 |
if(self.__image_format.endswith(ImageFormat.JPG)): |
191 |
self.__command = 'jpeg' |
192 |
elif(self.__image_format.endswith(ImageFormat.BMP)): |
193 |
self.__command = ImageFormat.BMP |
194 |
elif(self.__image_format.endswith(ImageFormat.PNM)): |
195 |
self.__command = ImageFormat.PNM |
196 |
elif(self.__image_format.endswith(ImageFormat.PNG)): |
197 |
self.__command = ImageFormat.PNG |
198 |
elif(self.__image_format.endswith(ImageFormat.TIF)): |
199 |
self.__command = 'tiff' |
200 |
else: |
201 |
raise IOError("ERROR: '" + self.__image_format + \ |
202 |
"' is an invalid image format.") |
203 |
|
204 |
return |
205 |
|
206 |
|
207 |
def __generateParameterFile(self): |
208 |
""" |
209 |
Write the list of parameters into the file. |
210 |
""" |
211 |
|
212 |
if(self.__image_range_used == True): # Image range was provided. |
213 |
input = self.__image_prefix + '*.' + self.__image_format + ' [' + \ |
214 |
self.__first_image_number + '-' + \ |
215 |
self.__last_image_number + ']\n' |
216 |
else: # Image list was provided |
217 |
input = self.__images |
218 |
|
219 |
parameter_file = open(self.__parameter_file, 'w') |
220 |
|
221 |
if os.name == 'nt' : |
222 |
tmp_input_name = self.__input_directory.replace('\\','/') |
223 |
tmp_movie_name = self.__movie.replace('\\','/') |
224 |
else: |
225 |
tmp_input_name = self.__input_directory |
226 |
tmp_movie_name = self.__movie |
227 |
|
228 |
parameter_file.write('PATTERN IBBPBBPBBPBBPBBP\n' + |
229 |
'OUTPUT ' + tmp_movie_name + '\n' |
230 |
'BASE_FILE_FORMAT PNM\n' + |
231 |
'INPUT_CONVERT ' + self.__command + 'topnm *\n' + |
232 |
'GOP_SIZE 16\n' + |
233 |
'SLICES_PER_FRAME 10\n' + |
234 |
'INPUT_DIR ' + tmp_input_name + '\n' + |
235 |
'INPUT\n' + |
236 |
input + |
237 |
'END_INPUT\n' + |
238 |
'PIXEL HALF\n' + |
239 |
'RANGE 10\n' + |
240 |
'PSEARCH_ALG LOGARITHMIC\n' + |
241 |
'BSEARCH_ALG CROSS2\n' + |
242 |
'IQSCALE 8\n' + |
243 |
'PQSCALE 10\n' + |
244 |
'BQSCALE 25\n' + |
245 |
'REFERENCE_FRAME DECODED\n' + |
246 |
'FORCE_ENCODE_LAST_FRAME\n' + |
247 |
'ASPECT_RATIO 1\n' + |
248 |
'FRAME_RATE 24') |
249 |
|
250 |
parameter_file.close() |
251 |
if os.name == 'nt' : |
252 |
tmp_name = self.__parameter_file.replace('\\','/') |
253 |
os.system('dos2unix ' + tmp_name) |
254 |
|
255 |
return |
256 |
|
257 |
|