~nico-inattendu/luciole/0.8_translation

« back to all changes in this revision

Viewing changes to lucioLib/luciole_import.py

  • Committer: nico at inattendu
  • Date: 2009-03-08 18:06:41 UTC
  • Revision ID: nico@inattendu.org-20090308180641-a9hf66kntqber0pf
Intial Import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
#
 
5
# Copyright Nicolas Bertrand (nico@inattendu.org), 2009
 
6
#
 
7
# This file is part of Luciole.
 
8
#
 
9
#    Luciole is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU General Public License as published by
 
11
#    the Free Software Foundation, either version 3 of the License, or
 
12
#    (at your option) any later version.
 
13
#
 
14
#    Luciole is distributed in the hope that it will be useful,
 
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#    GNU General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU General Public License
 
20
#    along with Luciole.  If not, see <http://www.gnu.org/licenses/>.
 
21
#
 
22
#
 
23
 
 
24
"""
 
25
luciole_import.py 
 
26
 
 
27
Started on  ven 28 déc 2007 12:57:52 @USER-NAME@
 
28
Last update ven 28 déc 2007 12:57:52 @USER-NAME@
 
29
 
 
30
@Author Nico 
 
31
@version 1
 
32
 
 
33
COMMENT : only PNG and JPEG are supportted.
 
34
"""
 
35
import gtk
 
36
import gtk.glade
 
37
import os.path
 
38
import os
 
39
import luciole_global as MG
 
40
import luciole_file_in_out as MF
 
41
import luciole_constants as MCONST
 
42
import luciole_class as MC
 
43
import Image
 
44
 
 
45
class luciole_import(object) :
 
46
  """ Manage the import of images. COMMENT : only PNG and JPEG are supported.
 
47
"""
 
48
 
 
49
  def __init__(self,gladefile,windowname):
 
50
    """ Init of module """
 
51
    self._gladefile=gladefile
 
52
    self._windowname=windowname
 
53
    self._tvCapture = MG.luciole_global.dico[MC.LucioleGtkTreeView.__name__] 
 
54
 
 
55
    self._inputImageList =[]
 
56
    self._outputImageList =[]
 
57
  def gui_import(self) :
 
58
    """ Import from gui. """
 
59
    if ( (MF.luciole_project_file.__name__ in MG.luciole_global.dico)
 
60
        and
 
61
        ( MG.luciole_global.dico[MF.luciole_project_file.__name__].ProjectLoaded) ) :
 
62
      self._Project = MG.luciole_global.dico[MF.luciole_project_file.__name__]
 
63
      # clear imageList when a project is Loaded
 
64
      self._inputImageList =[]
 
65
      self._outputImageList =[]
 
66
      # a project exists and it loaded, the fileChosser Window can be opened
 
67
      self.openFileWindow=gtk.glade.XML(self._gladefile,self._windowname)
 
68
      dialog = self.openFileWindow.get_widget(self._windowname)
 
69
      # set user dir as default dir
 
70
      dialog.set_current_folder(os.path.expandvars('$HOME'))    
 
71
 
 
72
      #set filters All files and all mime type images
 
73
      # Image filter 
 
74
      filter = gtk.FileFilter()
 
75
      filter.add_mime_type("image/jpeg") 
 
76
      filter.set_name("Images ( jpeg  )")
 
77
      dialog.add_filter(filter)
 
78
 
 
79
      # all files
 
80
      filter = gtk.FileFilter()
 
81
      filter.add_pattern("*")
 
82
      filter.set_name("All files")
 
83
      dialog.add_filter(filter)
 
84
  
 
85
      response=dialog.run() 
 
86
      # value 10 choosen in glade file th standard respone gtk.REPONSE_OK = -5
 
87
      # provides no result. It look like a bug in glade ...
 
88
      if response == 10 :
 
89
        print "Ok clicked files selected :"
 
90
        self._inputImageList = dialog.get_filenames()
 
91
        self.processImageList()
 
92
      elif response == gtk.RESPONSE_CANCEL:
 
93
        print 'Cancel clicked'
 
94
      #on ferme la fenetre
 
95
      dialog.destroy()
 
96
    else :
 
97
      # No Project Loaded send a message.
 
98
      message = "Opération non permise quand un projet n'est pas chargé. Veuillez créer un nouveau porojet ou en charger un."
 
99
      MG.luciole_global.dico['appgui'].ErrMessage(message)
 
100
 
 
101
  def processImageList(self) :
 
102
    """ Process the Image list , transform and Append it in capture treeview"""
 
103
    for image in self._inputImageList :
 
104
      #transform image
 
105
      imageTransformed = self.Transform(image)
 
106
      #append image in treview
 
107
      if imageTransformed :
 
108
        self._tvCapture.AppendFromCapture(imageTransformed)
 
109
    
 
110
  def Transform(self,imagePath) :
 
111
    """ Transform an image in jpeg format and defined size""" 
 
112
    #open file
 
113
    print " Image Process : ",imagePath
 
114
    im = Image.open(imagePath)
 
115
    (base,ext) = os.path.splitext(imagePath)
 
116
    (rep,base) = os.path.split(base)
 
117
    print im.format, im.size, im.mode
 
118
    if im.format == "JPEG" and im.size == MCONST.VIDEO_PAL_RES :
 
119
      print " Nothing to do image ",imagePath," is in correct format"
 
120
      IsImageTransformed =False
 
121
    if im.size != MCONST.VIDEO_PAL_RES :
 
122
      # image need to be rescaled
 
123
      out = im.resize(MCONST.VIDEO_PAL_RES)
 
124
      IsImageTransformed = True
 
125
    # save image
 
126
    dirOut = os.path.join(self._Project.project_dir,"work")
 
127
    fileOut = base+".jpeg"
 
128
    fileOut = os.path.join(dirOut,fileOut)
 
129
    if IsImageTransformed :
 
130
      out.save(fileOut)
 
131
    else :
 
132
      #image not transformed just copy it
 
133
      MF.luciole_file_tools.copy(imagePath,fileOut)     
 
134
    return fileOut
 
135
  
 
136