Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/downloadmanager.py
diff options
context:
space:
mode:
authorGonzalo Odiard <godiard@sugarlabs.org>2010-10-07 12:49:34 (GMT)
committer Gonzalo Odiard <godiard@sugarlabs.org>2010-10-07 12:49:34 (GMT)
commitb63ad414f697bc2050296456194381c7a423d4d6 (patch)
treec38b8de437057ecf957215687ab012c598ff5e43 /downloadmanager.py
parent978cc1edf77fc88c6c80c75694f96e2848c02ef1 (diff)
generate preview image for downloaded images (SL#1106)
Diffstat (limited to 'downloadmanager.py')
-rw-r--r--downloadmanager.py50
1 files changed, 30 insertions, 20 deletions
diff --git a/downloadmanager.py b/downloadmanager.py
index 34e18e2..cec673e 100644
--- a/downloadmanager.py
+++ b/downloadmanager.py
@@ -194,10 +194,8 @@ class Download:
sniffed_mime_type = mime.get_for_file(self._target_file.path)
self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
- if self._mime_type in ('image/bmp','image/gif','image/jpeg','image/png','image/tiff'):
- self.dl_jobject.metadata['preview'] = self.__get_preview_image()
- else:
- self.dl_jobject.metadata['preview'] = ''
+ if self._check_image_mime_type():
+ self.dl_jobject.metadata['preview'] = self._get_preview_image()
datastore.write(self.dl_jobject,
transfer_ownership=True,
@@ -205,26 +203,38 @@ class Download:
error_handler=self._internal_save_error_cb,
timeout=360 * DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND)
- def __get_preview_image(self):
+ def _check_image_mime_type(self):
+ for pixbuf_format in gtk.gdk.pixbuf_get_formats():
+ if self._mime_type in pixbuf_format['mime_types']:
+ return True
+ return False
+
+ def _get_preview_image(self):
+ preview_width, preview_height = style.zoom(300), style.zoom(225)
+
pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
width, height = pixbuf.get_width(), pixbuf.get_height()
- preview_width = style.zoom(300)
- preview_height = style.zoom(225)
-
+ scale = 1
if (width > preview_width) or (height > preview_height):
- scale_x = float(width) / preview_width
- scale_y = float(height) / preview_height
- scale = max(scale_x,scale_y)
-
- pixbuf = pixbuf.scale_simple(float(width) / scale, height / scale,
- gtk.gdk.INTERP_BILINEAR)
- pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,pixbuf.get_has_alpha(),8,preview_width,preview_height)
- pixbuf2.fill(0xffffffff)
- margin_x = (preview_width - pixbuf.get_width()) / 2
- margin_y = (preview_height - pixbuf.get_height()) / 2
-
- pixbuf.copy_area(0,0,pixbuf.get_width(),pixbuf.get_height(),pixbuf2,margin_x,margin_y)
+ scale_x = preview_width / float(width)
+ scale_y = preview_height / float(height)
+ scale = min(scale_x, scale_y)
+
+ pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, \
+ pixbuf.get_has_alpha(), \
+ pixbuf.get_bits_per_sample(), \
+ preview_width, preview_height)
+ pixbuf2.fill(style.COLOR_WHITE.get_int())
+
+ margin_x = int((preview_width - (width * scale)) / 2)
+ margin_y = int((preview_height - (height * scale)) / 2)
+
+ pixbuf.scale(pixbuf2, margin_x, margin_y, \
+ preview_width - (margin_x * 2), \
+ preview_height - (margin_y * 2), \
+ margin_x, margin_y, scale, scale, \
+ gtk.gdk.INTERP_BILINEAR)
preview_data = []
def save_func(buf, data):