forked from openlp/openlp
improved image resizing speed
This commit is contained in:
commit
37cbb5cbf4
@ -137,13 +137,12 @@ def image_to_byte(image):
|
|||||||
# convert to base64 encoding so does not get missed!
|
# convert to base64 encoding so does not get missed!
|
||||||
return byte_array.toBase64()
|
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.
|
Resize an image to fit on the current screen.
|
||||||
|
|
||||||
``image``
|
``image_path``
|
||||||
The image to resize. It has to be either a ``QImage`` instance or the
|
The path to the image to resize.
|
||||||
path to the image.
|
|
||||||
|
|
||||||
``width``
|
``width``
|
||||||
The new image width.
|
The new image width.
|
||||||
@ -155,16 +154,27 @@ def resize_image(image, width, height, background=QtCore.Qt.black):
|
|||||||
The background colour defaults to black.
|
The background colour defaults to black.
|
||||||
"""
|
"""
|
||||||
log.debug(u'resize_image - start')
|
log.debug(u'resize_image - start')
|
||||||
if isinstance(image, QtGui.QImage):
|
if isinstance(image_path, QtGui.QImage):
|
||||||
preview = image
|
print u'wrong instance!'
|
||||||
else:
|
else:
|
||||||
preview = QtGui.QImage(image)
|
reader = QtGui.QImageReader(image_path)
|
||||||
if not preview.isNull():
|
# The image's ratio.
|
||||||
# Only resize if different size
|
image_ratio = float(reader.size().width()) / float(reader.size().height())
|
||||||
if preview.width() == width and preview.height == height:
|
resize_ratio = float(width) / float(height)
|
||||||
return preview
|
# Figure out the size we want to resize the image to (keep aspect ratio).
|
||||||
preview = preview.scaled(width, height, QtCore.Qt.KeepAspectRatio,
|
if image_ratio == resize_ratio:
|
||||||
QtCore.Qt.SmoothTransformation)
|
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:
|
||||||
|
# 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()
|
realw = preview.width()
|
||||||
realh = preview.height()
|
realh = preview.height()
|
||||||
# and move it to the centre of the preview space
|
# and move it to the centre of the preview space
|
||||||
|
Loading…
Reference in New Issue
Block a user