← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~googol-hush/openlp/thumb-creation into lp:openlp

 

Andreas Preikschat has proposed merging lp:~googol-hush/openlp/thumb-creation into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)

For more details, see:
https://code.launchpad.net/~googol-hush/openlp/thumb-creation/+merge/64002

Hello,

- Improved image resizing speed

Average time needed to resize an image:
trunk:  ~0.74s
branch: ~0.375s

In another test I created an image service item (20 images of a total size of 31.5MB):
trunk: 22s
branch: 10s

Note, the time taken is lower when the image ratio is the same as the display ratio. This is because we have to paint black bars on the right and left (lower and upper) sides.

I do not know if you classify this as bug or not.
-- 
https://code.launchpad.net/~googol-hush/openlp/thumb-creation/+merge/64002
Your team OpenLP Core is requested to review the proposed merge of lp:~googol-hush/openlp/thumb-creation into lp:openlp.
=== modified file 'openlp/core/lib/__init__.py'
--- openlp/core/lib/__init__.py	2011-05-26 17:56:08 +0000
+++ openlp/core/lib/__init__.py	2011-06-09 12:36:30 +0000
@@ -137,13 +137,12 @@
     # convert to base64 encoding so does not get missed!
     return byte_array.toBase64()
 
-def resize_image(image, width, height, background=QtCore.Qt.black):
+def resize_image(image_path, width, height, background=QtCore.Qt.black):
     """
     Resize an image to fit on the current screen.
 
-    ``image``
-        The image to resize. It has to be either a ``QImage`` instance or the
-        path to the image.
+    ``image_path``
+        The path to the image to resize.
 
     ``width``
         The new image width.
@@ -155,16 +154,24 @@
         The background colour defaults to black.
     """
     log.debug(u'resize_image - start')
-    if isinstance(image, QtGui.QImage):
-        preview = image
+    reader = QtGui.QImageReader(image_path)
+    # The image's ratio.
+    image_ratio = float(reader.size().width()) / float(reader.size().height())
+    resize_ratio = float(width) / float(height)
+    # Figure out the size we want to resize the image to (keep aspect ratio).
+    if image_ratio == resize_ratio:
+        size = QtCore.QSize(width, height)
+    elif image_ratio < resize_ratio:
+        # Use the image's height as reference for the new size.
+        size = QtCore.QSize(image_ratio * height, height)
     else:
-        preview = QtGui.QImage(image)
-    if not preview.isNull():
-        # Only resize if different size
-        if preview.width() == width and preview.height == height:
-            return preview
-        preview = preview.scaled(width, height, QtCore.Qt.KeepAspectRatio,
-            QtCore.Qt.SmoothTransformation)
+        # Use the image's width as reference for the new size.
+        size = QtCore.QSize(width, 1 / (image_ratio / width))
+    reader.setScaledSize(size)
+    preview = reader.read()
+    if image_ratio == resize_ratio:
+        # We neither need to centre the image nor add "bars" to the image.
+        return preview
     realw = preview.width()
     realh = preview.height()
     # and move it to the centre of the preview space


Follow ups