Beispiel #1
0
 def _Convert(self, profile):
     source = self.files.get(profile.source)
     if not source or not source.tempfile:
         # convert only if tempfile
         return False, ()
     if not source:
         return False, [_(u"Image not found: ") + profile.source]
     p = DvPath()
     p.SetUniqueTempFileName()
     p.SetExtension(profile.extension)
     destPath = str(p)
     
     try:
         try:
             source.file.seek(0)
         except:
             pass
         try:
             iObj = Image.open(source)
         except IOError:
             # no file to be converted
             return False, ()
         iObj = iObj.convert("RGB")
         
         # resize
         size = [profile.width, profile.height]
         if size[0] != 0 or size[1] != 0:
             if size[0] == 0:    
                 size[0] = size[1]
             elif size[1] == 0:    
                 size[1] = size[0]
             x, y = iObj.size
             if x > size[0]: y = y * size[0] / x; x = size[0]
             if y > size[1]: x = x * size[1] / y; y = size[1]
             size = x, y
         
         iObj = iObj.resize(size, Image.ANTIALIAS)
         iObj.save(destPath, profile.format)
         try:
             source.file.seek(0)
         except:
             pass
         
         # file meta data
         imgFile = open(destPath)
         filename = DvPath(profile.dest+"_"+source.filename)
         filename.SetExtension(profile.extension)
         file = File(filekey=profile.dest, 
                     filename=str(filename), 
                     file=imgFile,
                     size=p.GetSize(), 
                     path=destPath, 
                     extension=profile.extension,  
                     tempfile=True)
         self.files.set(profile.dest, file)
     finally:
         # clean temp file
         p.Delete()
     return True, []
Beispiel #2
0
 def test_Get(self):
     n = self.name
     p = DvPath(n)
     self.assert_(p.GetPath() == self.base)
     self.assert_(p.GetName() == "nofile")
     self.assert_(p.GetExtension() == "txt")
     self.assert_(p.GetNameExtension() == "nofile.txt")
     self.assert_(p.GetSize() == -1)
     self.assert_(p.GetLastDirectory() == "tmp_nivepathtest_000")
     p.IsFile()
     p.IsDirectory()
     p.Exists()
     p.IsValid()
Beispiel #3
0
 def fromPath(self, path):
     """
     Set temp file and load file values from file path
     """
     if self.filename == "":
         self.filename = os.path.basename(path)
         f, fileExtension = os.path.splitext(path)
         self.extension = fileExtension[1:6]
     if self.size == 0:
         p = DvPath(path)
         self.size = p.GetSize()
     self.tempfile = True
     self.file = open(path, "rb")