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