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
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
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
59 if doResize or doReformat:
60 log.debug("Photo: Saving photo:%s Format:%s" % (out_file,format))
61 pb.save(out_file, format)
62
63 photo.force_new_file_extension(".%s" % format)
64
66 log.info("Transcode Photo: %s" % kwargs)
67
68
69 formats = kwargs.get("formats",photo.get_mimetype()).split(',')
70 newSize = kwargs.get("size",NO_RESIZE)
71
72
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
88 mimeType = photo.get_mimetype()
89 doReformat = False
90 if mimeType not in formats:
91
92 mimeType = kwargs.get("default-format","image/jpeg")
93 doReformat = True
94
95
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
119