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

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

  ViewVC Help
Powered by ViewVC 1.1.26