/[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 1613 - (show annotations)
Mon Jun 16 17:10:04 2008 UTC (14 years ago) by phornby
File MIME type: text/x-python
File size: 6834 byte(s)
Quick edit to track the ppmtompeg problem on windows.


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 from os import system
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
47
48 def imageRange(self, input_directory, first_image, last_image):
49 """
50 The image range from which the movie is to be generated from.
51
52 @type input_directory: String
53 @param input_directory: Directory in which the series of images can
54 be found
55 @type first_image: String
56 @param first_image: First image name (including the extension)
57 @type last_image: String
58 @param last_image: Last image name (including the extension)
59 """
60
61 # Keeps track whether an image range or image list was provided as
62 # the source for generating the movie.
63 self.__image_range_used = True
64 self.__input_directory = input_directory
65 self.__first_image = first_image
66 self.__last_image = last_image
67
68 self.__splitInput()
69 self.__retrieveFirstImageDetails()
70 self.__retrieveLastImageDetails()
71 self.__retrieveConversionCommand()
72
73 def imageList(self, input_directory, image_list):
74 """
75 The image list from which the movie is to be generated from.
76
77 @type input_directory: String
78 @param input_directory: Directory in which the series of images can
79 be found
80 @type image_list: List
81 @param image_list: List of images name (including the extension)
82 """
83
84 self.__image_range_used = False
85 self.__input_directory = input_directory
86 self.__images = ""
87
88 self.__first_image = image_list[0] # Get first image in the list.
89 self.__splitInput()
90 self.__retrieveConversionCommand()
91
92 for i in image_list:
93 self.__images += i + '\n'
94
95 def makeMovie(self, movie):
96 """
97 Generate the movie.
98
99 @type movie : String
100 @param movie: Movie name (including the .mpg extension)
101 """
102
103 self.__movie = movie
104 self.__generateParameterFile()
105
106 # If a parameter file name was not specified, then the default file
107 # will be deleted automatically once the movie has been generated.
108 # However, if a paramter file name was specified, the file will be
109 # maintained.
110 print '#'*50
111 print 'ppmtompeg ' + self.__parameter_file
112 system('ppmtompeg ' + self.__parameter_file)
113 if(self.__parameter_file == "make_movie"):
114 system('rm ' + self.__parameter_file)
115
116 def __splitInput(self):
117 """
118 Split the image label (i.e. temp-0001.jpg) to separate the
119 image name (i.e. temp-0001) and image format (i.e. jpg).
120 """
121
122 first_image_split = self.__first_image.split('.')
123 # Image format (i.e. jpg)
124 self.__image_format = first_image_split[len(first_image_split) - 1]
125 # First image name.
126 self.__first_image = \
127 self.__first_image.rstrip('.' + self.__image_format)
128
129 if(self.__image_range_used == True):
130 # Last image name.
131 self.__last_image = \
132 self.__last_image.rstrip('.' + self.__image_format)
133
134 def __retrieveFirstImageDetails(self):
135 """
136 Retrieve the first image name details.
137 """
138
139 # For a image called 'temp-0001.jpg', self.__first_image_number
140 # will be '0001', while self.__image_prefix will be 'temp-'.
141 self.__first_image_number = ""
142 self.__image_prefix = ""
143
144 for i in range(len(self.__first_image)):
145 if(self.__first_image[i].isdigit()): # Retrieve first image number.
146 self.__first_image_number = \
147 self.__first_image_number + self.__first_image[i]
148 else: # Retrieve image prefix.
149 self.__image_prefix = \
150 self.__image_prefix + self.__first_image[i]
151
152 def __retrieveLastImageDetails(self):
153 """
154 Retrieve the last image name details.
155 """
156
157 self.__last_image_number = ""
158
159 for i in range(len(self.__last_image)):
160 if(self.__last_image[i].isdigit()): # Retrieve last image number.
161 self.__last_image_number = \
162 self.__last_image_number + self.__last_image[i]
163
164 def __retrieveConversionCommand(self):
165 """
166 Retrieve the conversion command (depending on the image format)
167 required by the 'ppmtompeg' command.
168 """
169
170 if(self.__image_format.endswith(ImageFormat.JPG)):
171 self.__command = 'jpeg'
172 elif(self.__image_format.endswith(ImageFormat.BMP)):
173 self.__command = ImageFormat.BMP
174 elif(self.__image_format.endswith(ImageFormat.PNM)):
175 self.__command = ImageFormat.PNM
176 elif(self.__image_format.endswith(ImageFormat.PNG)):
177 self.__command = ImageFormat.PNG
178 elif(self.__image_format.endswith(ImageFormat.TIF)):
179 self.__command = 'tiff'
180 else:
181 raise IOError("ERROR: '" + self.__image_format + \
182 "' is an invalid image format.")
183
184
185 def __generateParameterFile(self):
186 """
187 Write the list of parameters into the file.
188 """
189
190 if(self.__image_range_used == True): # Image range was provided.
191 input = self.__image_prefix + '*.' + self.__image_format + ' [' + \
192 self.__first_image_number + '-' + \
193 self.__last_image_number + ']\n'
194 else: # Image list was provided
195 input = self.__images
196
197 parameter_file = open(self.__parameter_file, 'w')
198
199 parameter_file.write('PATTERN IBBPBBPBBPBBPBBP\n' +
200 'OUTPUT ' + self.__movie + '\n'
201 'BASE_FILE_FORMAT PNM\n' +
202 'INPUT_CONVERT ' + self.__command + 'topnm *\n' +
203 'GOP_SIZE 16\n' +
204 'SLICES_PER_FRAME 10\n' +
205 'INPUT_DIR ' + self.__input_directory + '\n' +
206 'INPUT\n' +
207 input +
208 'END_INPUT\n' +
209 'PIXEL HALF\n' +
210 'RANGE 10\n' +
211 'PSEARCH_ALG LOGARITHMIC\n' +
212 'BSEARCH_ALG CROSS2\n' +
213 'IQSCALE 8\n' +
214 'PQSCALE 10\n' +
215 'BQSCALE 25\n' +
216 'REFERENCE_FRAME DECODED\n' +
217 'FORCE_ENCODE_LAST_FRAME\n' +
218 'ASPECT_RATIO 1\n' +
219 'FRAME_RATE 24')
220
221 parameter_file.close()
222

  ViewVC Help
Powered by ViewVC 1.1.26