/[escript]/trunk/pyvisi/py_src/movie.py
ViewVC logotype

Contents of /trunk/pyvisi/py_src/movie.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2881 - (show annotations)
Thu Jan 28 02:03:15 2010 UTC (11 years, 1 month ago) by jfenwick
File MIME type: text/x-python
File size: 10577 byte(s)
Don't panic.
Updating copyright stamps

1
2 ########################################################
3 #
4 # Copyright (c) 2003-2010 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-2010 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__="https://launchpad.net/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 from esys.escript import getMPIWorldMax, getMPIRankWorld
36 import os
37
38
39 class Movie:
40 """
41 Class that creates a file called 'make_movie' by default (if a parameter
42 file name is not speficied) which contains a list of parameters required
43 by the 'ppmtompeg' command to generate a movie from a series of images.
44
45 :attention: A movie cannot be generated from postscript (.ps) images.
46 """
47
48 def __init__(self, parameter_file = "make_movie"):
49 """
50 Initialise the generation of the movie. If a parameter file name
51 is supplied, the file will not be deleted once the movie has been
52 generated. Otherwise, the temporary parameter file created will
53 automatically be removed at the end.
54
55 :type parameter_file: String
56 :param parameter_file: Name of the file containing the list of
57 parameters required by the 'ppmtompeg' command.
58 """
59
60 self.__parameter_file = parameter_file
61 return
62
63
64 def imageRange(self, input_directory, first_image, last_image):
65 """
66 The image range from which the movie is to be generated from.
67
68 :type input_directory: String
69 :param input_directory: Directory in which the series of images can be found
70 :type first_image: String
71 :param first_image: First image name (including the extension)
72 :type last_image: String
73 :param last_image: Last image name (including the extension)
74 """
75
76 # Keeps track whether an image range or image list was provided as
77 # the source for generating the movie.
78 self.__image_range_used = True
79 self.__input_directory = input_directory
80 self.__first_image = first_image
81 self.__last_image = last_image
82
83 self.__splitInput()
84 self.__retrieveFirstImageDetails()
85 self.__retrieveLastImageDetails()
86 self.__retrieveConversionCommand()
87 return
88
89 def imageList(self, input_directory, image_list):
90 """
91 The image list from which the movie is to be generated from.
92
93 :type input_directory: String
94 :param input_directory: Directory in which the series of images can 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 if getMPIRankWorld() == 0:
127 ret = os.system('ppmtompeg ' + self.__parameter_file) / 256
128 else:
129 ret=0
130 ret=getMPIWorldMax(ret)
131 if ret > 0:
132 raise RuntimeError, "Could not generate movie %s"%'ppmtompeg ' + self.__parameter_file
133
134 if(self.__parameter_file == "make_movie"):
135 if getMPIRankWorld() == 0:
136 ret = os.system('rm ' + self.__parameter_file) / 256
137 else:
138 ret=0
139 ret=getMPIWorldMax(ret)
140 if ret > 0:
141 raise RuntimeError, 'Could not remove file '+ self.__parameter_file
142 return
143
144 def __splitInput(self):
145 """
146 Split the image label (i.e. temp-0001.jpg) to separate the
147 image name (i.e. temp-0001) and image format (i.e. jpg).
148 """
149
150 first_image_split = self.__first_image.split('.')
151 # Image format (i.e. jpg)
152 self.__image_format = first_image_split[len(first_image_split) - 1]
153 # First image name.
154 self.__first_image = \
155 self.__first_image.rstrip('.' + self.__image_format)
156
157 if(self.__image_range_used == True):
158 # Last image name.
159 self.__last_image = \
160 self.__last_image.rstrip('.' + self.__image_format)
161 return
162
163 def __retrieveFirstImageDetails(self):
164 """
165 Retrieve the first image name details.
166 """
167
168 # For a image called 'temp-0001.jpg', self.__first_image_number
169 # will be '0001', while self.__image_prefix will be 'temp-'.
170 self.__first_image_number = ""
171 self.__image_prefix = ""
172
173 for i in range(len(self.__first_image)):
174 if(self.__first_image[i].isdigit()): # Retrieve first image number.
175 self.__first_image_number = \
176 self.__first_image_number + self.__first_image[i]
177 else: # Retrieve image prefix.
178 self.__image_prefix = \
179 self.__image_prefix + self.__first_image[i]
180 return
181
182 def __retrieveLastImageDetails(self):
183 """
184 Retrieve the last image name details.
185 """
186
187 self.__last_image_number = ""
188
189 for i in range(len(self.__last_image)):
190 if(self.__last_image[i].isdigit()): # Retrieve last image number.
191 self.__last_image_number = \
192 self.__last_image_number + self.__last_image[i]
193
194 return
195
196 def __retrieveConversionCommand(self):
197 """
198 Retrieve the conversion command (depending on the image format)
199 required by the 'ppmtompeg' command.
200 """
201
202 if(self.__image_format.endswith(ImageFormat.JPG)):
203 self.__command = 'jpeg'
204 elif(self.__image_format.endswith(ImageFormat.BMP)):
205 self.__command = ImageFormat.BMP
206 elif(self.__image_format.endswith(ImageFormat.PNM)):
207 self.__command = ImageFormat.PNM
208 elif(self.__image_format.endswith(ImageFormat.PNG)):
209 self.__command = ImageFormat.PNG
210 elif(self.__image_format.endswith(ImageFormat.TIF)):
211 self.__command = 'tiff'
212 else:
213 raise IOError("ERROR: '" + self.__image_format + \
214 "' is an invalid image format.")
215
216 return
217
218
219 def __generateParameterFile(self):
220 """
221 Write the list of parameters into the file.
222 """
223
224 if(self.__image_range_used == True): # Image range was provided.
225 input = self.__image_prefix + '*.' + self.__image_format + ' [' + \
226 self.__first_image_number + '-' + \
227 self.__last_image_number + ']\n'
228 else: # Image list was provided
229 input = self.__images
230
231
232 ret=0
233 if getMPIRankWorld() == 0:
234 try:
235 parameter_file = open(self.__parameter_file, 'w')
236
237 if os.name == 'nt' :
238 tmp_input_name = self.__input_directory.replace('\\','/')
239 tmp_movie_name = self.__movie.replace('\\','/')
240 else:
241 tmp_input_name = self.__input_directory
242 tmp_movie_name = self.__movie
243
244 parameter_file.write('PATTERN IBBPBBPBBPBBPBBP\n' +
245 'OUTPUT ' + tmp_movie_name + '\n'
246 'BASE_FILE_FORMAT PNM\n' +
247 'INPUT_CONVERT ' + self.__command + 'topnm *\n' +
248 'GOP_SIZE 16\n' +
249 'SLICES_PER_FRAME 10\n' +
250 'INPUT_DIR ' + tmp_input_name + '\n' +
251 'INPUT\n' +
252 input +
253 'END_INPUT\n' +
254 'PIXEL HALF\n' +
255 'RANGE 10\n' +
256 'PSEARCH_ALG LOGARITHMIC\n' +
257 'BSEARCH_ALG CROSS2\n' +
258 'IQSCALE 8\n' +
259 'PQSCALE 10\n' +
260 'BQSCALE 25\n' +
261 'REFERENCE_FRAME DECODED\n' +
262 'FORCE_ENCODE_LAST_FRAME\n' +
263 'ASPECT_RATIO 1\n' +
264 'FRAME_RATE 24')
265
266 parameter_file.close()
267 if os.name == 'nt':
268 tmp_name = self.__parameter_file.replace('\\','/')
269 ret = os.system('dos2unix ' + tmp_name) / 256
270 if ret >0:
271 raise RuntimeError,"execution of %s has failed."%('dos2unix ' + tmp_name)
272 except Exception, e:
273 ret=1
274 ret=getMPIWorldMax(ret)
275 if ret > 0:
276 if getMPIRankWorld() == 0:
277 raise e
278 else:
279 raise RuntimeError, "Movie generation on processor 0 failed."
280 return

  ViewVC Help
Powered by ViewVC 1.1.26