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 |
@type 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 |
system('ppmtompeg ' + self.__parameter_file) |
111 |
if(self.__parameter_file == "make_movie"): |
112 |
system('rm ' + self.__parameter_file) |
113 |
|
114 |
def __splitInput(self): |
115 |
""" |
116 |
Split the image label (i.e. temp-0001.jpg) to separate the |
117 |
image name (i.e. temp-0001) and image format (i.e. jpg). |
118 |
""" |
119 |
|
120 |
first_image_split = self.__first_image.split('.') |
121 |
# Image format (i.e. jpg) |
122 |
self.__image_format = first_image_split[len(first_image_split) - 1] |
123 |
# First image name. |
124 |
self.__first_image = \ |
125 |
self.__first_image.rstrip('.' + self.__image_format) |
126 |
|
127 |
if(self.__image_range_used == True): |
128 |
# Last image name. |
129 |
self.__last_image = \ |
130 |
self.__last_image.rstrip('.' + self.__image_format) |
131 |
|
132 |
def __retrieveFirstImageDetails(self): |
133 |
""" |
134 |
Retrieve the first image name details. |
135 |
""" |
136 |
|
137 |
# For a image called 'temp-0001.jpg', self.__first_image_number |
138 |
# will be '0001', while self.__image_prefix will be 'temp-'. |
139 |
self.__first_image_number = "" |
140 |
self.__image_prefix = "" |
141 |
|
142 |
for i in range(len(self.__first_image)): |
143 |
if(self.__first_image[i].isdigit()): # Retrieve first image number. |
144 |
self.__first_image_number = \ |
145 |
self.__first_image_number + self.__first_image[i] |
146 |
else: # Retrieve image prefix. |
147 |
self.__image_prefix = \ |
148 |
self.__image_prefix + self.__first_image[i] |
149 |
|
150 |
def __retrieveLastImageDetails(self): |
151 |
""" |
152 |
Retrieve the last image name details. |
153 |
""" |
154 |
|
155 |
self.__last_image_number = "" |
156 |
|
157 |
for i in range(len(self.__last_image)): |
158 |
if(self.__last_image[i].isdigit()): # Retrieve last image number. |
159 |
self.__last_image_number = \ |
160 |
self.__last_image_number + self.__last_image[i] |
161 |
|
162 |
def __retrieveConversionCommand(self): |
163 |
""" |
164 |
Retrieve the conversion command (depending on the image format) |
165 |
required by the 'ppmtompeg' command. |
166 |
""" |
167 |
|
168 |
if(self.__image_format.endswith(ImageFormat.JPG)): |
169 |
self.__command = 'jpeg' |
170 |
elif(self.__image_format.endswith(ImageFormat.BMP)): |
171 |
self.__command = ImageFormat.BMP |
172 |
elif(self.__image_format.endswith(ImageFormat.PNM)): |
173 |
self.__command = ImageFormat.PNM |
174 |
elif(self.__image_format.endswith(ImageFormat.PNG)): |
175 |
self.__command = ImageFormat.PNG |
176 |
elif(self.__image_format.endswith(ImageFormat.TIF)): |
177 |
self.__command = 'tiff' |
178 |
else: |
179 |
raise IOError("ERROR: '" + self.__image_format + \ |
180 |
"' is an invalid image format.") |
181 |
|
182 |
|
183 |
def __generateParameterFile(self): |
184 |
""" |
185 |
Write the list of parameters into the file. |
186 |
""" |
187 |
|
188 |
if(self.__image_range_used == True): # Image range was provided. |
189 |
input = self.__image_prefix + '*.' + self.__image_format + ' [' + \ |
190 |
self.__first_image_number + '-' + \ |
191 |
self.__last_image_number + ']\n' |
192 |
else: # Image list was provided |
193 |
input = self.__images |
194 |
|
195 |
parameter_file = open(self.__parameter_file, 'w') |
196 |
|
197 |
parameter_file.write('PATTERN IBBPBBPBBPBBPBBP\n' + |
198 |
'OUTPUT ' + self.__movie + '\n' |
199 |
'BASE_FILE_FORMAT PNM\n' + |
200 |
'INPUT_CONVERT ' + self.__command + 'topnm *\n' + |
201 |
'GOP_SIZE 16\n' + |
202 |
'SLICES_PER_FRAME 10\n' + |
203 |
'INPUT_DIR ' + self.__input_directory + '\n' + |
204 |
'INPUT\n' + |
205 |
input + |
206 |
'END_INPUT\n' + |
207 |
'PIXEL HALF\n' + |
208 |
'RANGE 10\n' + |
209 |
'PSEARCH_ALG LOGARITHMIC\n' + |
210 |
'BSEARCH_ALG CROSS2\n' + |
211 |
'IQSCALE 8\n' + |
212 |
'PQSCALE 10\n' + |
213 |
'BQSCALE 25\n' + |
214 |
'REFERENCE_FRAME DECODED\n' + |
215 |
'FORCE_ENCODE_LAST_FRAME\n' + |
216 |
'ASPECT_RATIO 1\n' + |
217 |
'FRAME_RATE 24') |
218 |
|
219 |
parameter_file.close() |
220 |
|