/[escript]/branches/windows_from_1456_trunk_1580_merged_in/pyvisi/py_src/movie.py
ViewVC logotype

Contents of /branches/windows_from_1456_trunk_1580_merged_in/pyvisi/py_src/movie.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1616 - (show annotations)
Tue Jun 17 10:42:36 2008 UTC (13 years, 4 months ago) by phornby
File MIME type: text/x-python
File size: 8870 byte(s)
Remove tabs & use standard 4 space indent. Cannot yet test on linux because of vtk problems.


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

  ViewVC Help
Powered by ViewVC 1.1.26