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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1615 by trankine, Tue Jun 17 10:22:20 2008 UTC revision 1616 by phornby, Tue Jun 17 10:42:36 2008 UTC
# Line 22  from esys.pyvisi.constant import ImageFo Line 22  from esys.pyvisi.constant import ImageFo
22  import os  import os
23    
24  class Movie:  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          Class that creates a file called 'make_movie' by default (if a parameter          Split the image label (i.e. temp-0001.jpg) to separate the
122          file name is not speficied) which contains a list of parameters required          image name (i.e. temp-0001) and image format (i.e. jpg).
123          by the 'ppmtompeg' command to generate a movie from a series of images.          """
124    
125          @attention: A movie cannot be generated from postscript (.ps) images.          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          def __init__(self, parameter_file = "make_movie"):                # First image name.
129                  """          self.__first_image = \
130                  Initialise the generation of the movie. If a parameter file name                             self.__first_image.rstrip('.' + self.__image_format)
131                  is supplied, the file will not be deleted once the movie has been  
132                  generated. Otherwise, the temporary parameter file created will          if(self.__image_range_used == True):
133                  automatically be removed at the end.              # Last image name.
134                self.__last_image = \
135                  @type parameter_file: String                                self.__last_image.rstrip('.' + self.__image_format)
136                  @param parameter_file: Name of the file containing the list of          return
137              parameters required by the 'ppmtompeg' command.  
138                  """      def __retrieveFirstImageDetails(self):
139            """
140                  self.__parameter_file = parameter_file          Retrieve the first image name details.
141            """
142    
143          def imageRange(self, input_directory, first_image, last_image):          # For a image called 'temp-0001.jpg', self.__first_image_number
144                  """          # will be '0001', while self.__image_prefix will be 'temp-'.
145                  The image range from which the movie is to be generated from.          self.__first_image_number = ""
146            self.__image_prefix = ""
147                  @type input_directory: String  
148                  @param input_directory: Directory in which the series of images can          for i in range(len(self.__first_image)):
149                          be found              if(self.__first_image[i].isdigit()): # Retrieve first image number.
150                  @type first_image: String                  self.__first_image_number = \
151                  @param first_image: First image name (including the extension)                                            self.__first_image_number + self.__first_image[i]
152                  @type last_image: String              else: # Retrieve image prefix.
153                  @param last_image: Last image name (including the extension)                  self.__image_prefix = \
154                  """                                      self.__image_prefix + self.__first_image[i]
155            return
156                  # Keeps track whether an image range or image list was provided as  
157                  # the source for generating the movie.      def __retrieveLastImageDetails(self):
158                  self.__image_range_used = True          """
159                  self.__input_directory = input_directory          Retrieve the last image name details.
160                  self.__first_image = first_image          """
161                  self.__last_image = last_image  
162            self.__last_image_number = ""
163                  self.__splitInput()  
164                  self.__retrieveFirstImageDetails()          for i in range(len(self.__last_image)):
165                  self.__retrieveLastImageDetails()              if(self.__last_image[i].isdigit()): # Retrieve last image number.
166                  self.__retrieveConversionCommand()                  self.__last_image_number = \
167                                             self.__last_image_number + self.__last_image[i]
168          def imageList(self, input_directory, image_list):  
169                  """          return
170                  The image list from which the movie is to be generated from.  
171        def __retrieveConversionCommand(self):
172                  @type input_directory: String          """
173                  @param input_directory: Directory in which the series of images can          Retrieve the conversion command (depending on the image format)
174                          be found          required by the 'ppmtompeg' command.
175                  @type image_list: List          """
176                  @param image_list: List of images name (including the extension)  
177                  """          if(self.__image_format.endswith(ImageFormat.JPG)):
178                self.__command = 'jpeg'
179                  self.__image_range_used = False          elif(self.__image_format.endswith(ImageFormat.BMP)):
180                  self.__input_directory = input_directory              self.__command = ImageFormat.BMP
181                  self.__images = ""          elif(self.__image_format.endswith(ImageFormat.PNM)):
182                self.__command = ImageFormat.PNM
183                  self.__first_image = image_list[0] # Get first image in the list.                        elif(self.__image_format.endswith(ImageFormat.PNG)):
184                  self.__splitInput()              self.__command = ImageFormat.PNG
185                  self.__retrieveConversionCommand()          elif(self.__image_format.endswith(ImageFormat.TIF)):
186                self.__command = 'tiff'
187                  for i in image_list:          else:
188                          self.__images += i  + '\n'              raise IOError("ERROR: '" + self.__image_format + \
189                              "' is an invalid image format.")
190          def makeMovie(self, movie):  
191                  """          return
192                  Generate the movie.  
193    
194                  @type movie : String      def __generateParameterFile(self):
195                  @param movie: Movie name (including the .mpg extension)          """
196                  """          Write the list of parameters into the file.
197            """
198                  self.__movie = movie  
199                  self.__generateParameterFile()          if(self.__image_range_used == True): # Image range was provided.
200                input = self.__image_prefix + '*.' + self.__image_format + ' [' + \
201                  # If a parameter file name was not specified, then the default file                      self.__first_image_number + '-' + \
202                  # will be deleted automatically once the movie has been generated.                      self.__last_image_number + ']\n'
203                  # However, if a paramter file name was specified, the file will be          else: # Image list was provided
204                  # maintained.              input = self.__images
205                  os.system('ppmtompeg ' + self.__parameter_file)  
206                  if(self.__parameter_file == "make_movie"):          parameter_file = open(self.__parameter_file, 'w')
207                          os.system('rm ' + self.__parameter_file)  
208            if os.name == 'nt' :
209          def __splitInput(self):              tmp_input_name = self.__input_directory.replace('\\','/')
210                  """              tmp_movie_name = self.__movie.replace('\\','/')
211                  Split the image label (i.e. temp-0001.jpg) to separate the          else:
212                  image name (i.e. temp-0001) and image format (i.e. jpg).              tmp_input_name = self.__input_directory
213                  """              tmp_movie_name = self.__movie
214    
215                  first_image_split = self.__first_image.split('.')          parameter_file.write('PATTERN IBBPBBPBBPBBPBBP\n' +
216                  # Image format (i.e. jpg)                               'OUTPUT ' + tmp_movie_name + '\n'
217                  self.__image_format = first_image_split[len(first_image_split) - 1]                               'BASE_FILE_FORMAT PNM\n' +
218                  # First image name.                               'INPUT_CONVERT ' +  self.__command + 'topnm *\n' +
219                  self.__first_image = \                               'GOP_SIZE 16\n' +
220                                  self.__first_image.rstrip('.' + self.__image_format)                               'SLICES_PER_FRAME 10\n' +
221                                 'INPUT_DIR ' + tmp_input_name + '\n' +
222                  if(self.__image_range_used == True):                               'INPUT\n' +
223                          # Last image name.                               input +
224                          self.__last_image = \                               'END_INPUT\n' +
225                                          self.__last_image.rstrip('.' + self.__image_format)                               'PIXEL HALF\n' +
226                                 'RANGE 10\n' +
227          def __retrieveFirstImageDetails(self):                               'PSEARCH_ALG LOGARITHMIC\n' +
228                  """                               'BSEARCH_ALG CROSS2\n' +
229                  Retrieve the first image name details.                               'IQSCALE 8\n' +
230                  """                               'PQSCALE 10\n' +
231                                 'BQSCALE 25\n' +
232                  # For a image called 'temp-0001.jpg', self.__first_image_number                               'REFERENCE_FRAME DECODED\n' +
233                  # will be '0001', while self.__image_prefix will be 'temp-'.                               'FORCE_ENCODE_LAST_FRAME\n' +
234                  self.__first_image_number = ""                               'ASPECT_RATIO 1\n' +
235                  self.__image_prefix = ""                               'FRAME_RATE 24')
236    
237                  for i in range(len(self.__first_image)):          parameter_file.close()
238                          if(self.__first_image[i].isdigit()): # Retrieve first image number.          if os.name == 'nt' :
239                                  self.__first_image_number = \              tmp_name = self.__parameter_file.replace('\\','/')
240                                                  self.__first_image_number + self.__first_image[i]              os.system('dos2unix ' + tmp_name)
241                          else: # Retrieve image prefix.  
242                                  self.__image_prefix = \          return
                                                 self.__image_prefix + self.__first_image[i]  
   
         def __retrieveLastImageDetails(self):  
                 """  
                 Retrieve the last image name details.  
                 """  
   
                 self.__last_image_number = ""  
   
                 for i in range(len(self.__last_image)):  
                         if(self.__last_image[i].isdigit()): # Retrieve last image number.  
                                 self.__last_image_number = \  
                                                 self.__last_image_number + self.__last_image[i]  
   
         def __retrieveConversionCommand(self):  
                 """  
                 Retrieve the conversion command (depending on the image format)  
                 required by the 'ppmtompeg' command.  
                 """  
   
                 if(self.__image_format.endswith(ImageFormat.JPG)):  
                         self.__command = 'jpeg'  
                 elif(self.__image_format.endswith(ImageFormat.BMP)):  
                         self.__command = ImageFormat.BMP  
                 elif(self.__image_format.endswith(ImageFormat.PNM)):  
                         self.__command = ImageFormat.PNM  
                 elif(self.__image_format.endswith(ImageFormat.PNG)):  
                         self.__command = ImageFormat.PNG  
                 elif(self.__image_format.endswith(ImageFormat.TIF)):  
                         self.__command = 'tiff'  
                 else:  
                         raise IOError("ERROR: '" + self.__image_format + \  
                                         "' is an invalid image format.")  
   
   
         def __generateParameterFile(self):  
                 """  
                 Write the list of parameters into the file.  
                 """  
   
                 if(self.__image_range_used == True): # Image range was provided.  
                         input = self.__image_prefix + '*.' + self.__image_format + ' [' + \  
                                         self.__first_image_number + '-' + \  
                                         self.__last_image_number + ']\n'  
                 else: # Image list was provided  
                         input = self.__images  
   
                 parameter_file = open(self.__parameter_file, 'w')  
   
                 if os.name == 'nt' :  
                         tmp_input_name = self.__input_directory.replace('\\','/')  
             tmp_movie_name = self.__movie.replace('\\','/')  
         else:  
                         tmp_input_name = self.__input_directory  
             tmp_movie_name = self.__movie  
   
                 parameter_file.write('PATTERN IBBPBBPBBPBBPBBP\n' +  
                         'OUTPUT ' + tmp_movie_name + '\n'  
                         'BASE_FILE_FORMAT PNM\n' +  
                         'INPUT_CONVERT ' +  self.__command + 'topnm *\n' +  
                         'GOP_SIZE 16\n' +  
                         'SLICES_PER_FRAME 10\n' +  
                         'INPUT_DIR ' + tmp_input_name + '\n' +  
                         'INPUT\n' +  
                         input +  
                         'END_INPUT\n' +  
                         'PIXEL HALF\n' +  
                         'RANGE 10\n' +  
                         'PSEARCH_ALG LOGARITHMIC\n' +  
                         'BSEARCH_ALG CROSS2\n' +  
                         'IQSCALE 8\n' +  
                         'PQSCALE 10\n' +  
                         'BQSCALE 25\n' +  
                         'REFERENCE_FRAME DECODED\n' +  
                         'FORCE_ENCODE_LAST_FRAME\n' +  
                         'ASPECT_RATIO 1\n' +  
                         'FRAME_RATE 24')  
   
                 parameter_file.close()  
                 if os.name == 'nt' :  
                         tmp_name = self.__parameter_file.replace('\\','/')  
                         os.system('dos2unix ' + tmp_name)  
243    
244    

Legend:
Removed from v.1615  
changed lines
  Added in v.1616

  ViewVC Help
Powered by ViewVC 1.1.26