Package conduit :: Package modules :: Module PhotoConverterModule
[hide private]

Source Code for Module conduit.modules.PhotoConverterModule

  1  import logging 
  2  log = logging.getLogger("modules.PhotoConverter") 
  3   
  4  import conduit 
  5  import conduit.utils as Utils 
  6  import conduit.TypeConverter as TypeConverter 
  7  import conduit.datatypes.File as File 
  8  import conduit.datatypes.Photo as Photo 
  9   
 10  MODULES = { 
 11          "PixbufPhotoConverter" :  { "type": "converter" } 
 12  } 
 13   
 14  NO_RESIZE = "None" 
 15   
16 -class PixbufPhotoConverter(TypeConverter.Converter):
17 - def __init__(self):
18 self.conversions = { 19 "file/photo,file/photo" : self.transcode, 20 "file,file/photo" : self.file_to_photo 21 } 22 self._image_types = None
23
24 - def _get_pixbuf_capabilities(self):
25 """ 26 Returns a dict mapping image mimetypes to extensions to 27 be used when converting image formats 28 """ 29 if self._image_types == None: 30 import gtk.gdk 31 types = {} 32 for f in gtk.gdk.pixbuf_get_formats(): 33 for t in f["mime_types"]: 34 if f["is_writable"] == True: 35 types[t] = f["extensions"][0] 36 else: 37 types[t] = None 38 self._image_types = types 39 return self._image_types
40
41 - def _convert(self, photo, format, width, height, doResize, doReformat):
42 """ 43 Basically we defer the conversion until as late as possible, or 44 not at all. 45 """ 46 import gtk.gdk 47 48 pb = photo.get_photo_pixbuf() 49 out_file = photo.to_tempfile() 50 51 if doResize: 52 try: 53 log.debug("Photo: Scaling to %sx%s" % (width,height)) 54 pb = pb.scale_simple(width,height,gtk.gdk.INTERP_HYPER) 55 except Exception, err: 56 log.debug("Photo: Error scaling photo\n%s" % err) 57 58 #save to new format. gdk.Pixbuf needs the type argument 59 if doResize or doReformat: 60 log.debug("Photo: Saving photo:%s Format:%s" % (out_file,format)) 61 pb.save(out_file, format) 62 #can safely rename the file here because its defintately a tempfile 63 photo.force_new_file_extension(".%s" % format)
64
65 - def transcode(self, photo, **kwargs):
66 log.info("Transcode Photo: %s" % kwargs) 67 68 #default format is the current format, and default is no resize 69 formats = kwargs.get("formats",photo.get_mimetype()).split(',') 70 newSize = kwargs.get("size",NO_RESIZE) 71 72 #resize if necessary 73 if newSize != NO_RESIZE: 74 w,h = photo.get_photo_size() 75 width,height = Utils.get_proportional_resize( 76 desiredW=int(newSize.split('x')[0]), 77 desiredH=int(newSize.split('x')[1]), 78 currentW=int(w), 79 currentH=int(h) 80 ) 81 doResize = True 82 else: 83 width = None 84 height = None 85 doResize = False 86 87 #check if the photo is in the allowed format, otherwise we must convert it 88 mimeType = photo.get_mimetype() 89 doReformat = False 90 if mimeType not in formats: 91 #convert photo to default format 92 mimeType = kwargs.get("default-format","image/jpeg") 93 doReformat = True 94 95 #convert the mimetype to the image type for gdk pixbuf save method 96 format = self._get_pixbuf_capabilities()[mimeType] 97 98 self._convert( 99 photo, 100 format, 101 width, 102 height, 103 doResize, 104 doReformat 105 ) 106 107 return photo
108
109 - def file_to_photo(self, f, **kwargs):
110 t = f.get_mimetype() 111 if t in self._get_pixbuf_capabilities().keys(): 112 p = Photo.Photo( 113 URI=f._get_text_uri() 114 ) 115 p.set_from_instance(f) 116 return self.transcode(p,**kwargs) 117 else: 118 return None
119