From b28a054c9074efc068e9238276533a197ba4b888 Mon Sep 17 00:00:00 2001 From: BasioMeusPuga Date: Mon, 20 Nov 2017 09:10:10 +0530 Subject: [PATCH] Resize cover images --- library.py | 4 +--- sorter.py | 30 +++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/library.py b/library.py index cd84f51..8dec2d4 100644 --- a/library.py +++ b/library.py @@ -32,7 +32,6 @@ class Library: print('Database returned nothing') return - elif mode == 'addition': # Assumes parent_window.viewModel already exists and may be extended # Because any additional books have already been added to the @@ -57,7 +56,6 @@ class Library: else: return - for i in books: # The database query returns (or the extension data is) # an iterable with the following indices: @@ -106,7 +104,7 @@ class Library: img_pixmap.loadFromData(cover) else: img_pixmap.load(':/images/NotFound.png') - img_pixmap = img_pixmap.scaled(420, 600, QtCore.Qt.IgnoreAspectRatio) + img_pixmap = img_pixmap.scaled(420, 600, QtCore.Qt.IgnoreAspectRatio) item = QtGui.QStandardItem() item.setToolTip(tooltip_string) # The following order is needed to keep sorting working diff --git a/sorter.py b/sorter.py index ec4f83d..d6efec6 100644 --- a/sorter.py +++ b/sorter.py @@ -3,11 +3,12 @@ # TODO # See if you want to include a hash of the book's name and author +import io import os import pickle import hashlib from multiprocessing.dummy import Pool -from PyQt5 import QtCore +from PyQt5 import QtCore, QtGui import database @@ -141,10 +142,13 @@ class BookSorter: # Different modes require different values if self.mode == 'addition': - cover_image = book_ref.get_cover_image() - # TODO - # Consider sizing down the image in case - # it's too big + cover_image_raw = book_ref.get_cover_image() + if cover_image_raw: + # Reduce the size of the incoming image + cover_image = resize_image(cover_image_raw) + else: + cover_image = None + self.all_books[file_md5] = { 'title': title, 'author': author, @@ -188,3 +192,19 @@ class BookSorter: _pool.join() return self.all_books + + +def resize_image(cover_image_raw): + cover_image = QtGui.QImage() + cover_image.loadFromData(cover_image_raw) + cover_image = cover_image.scaled( + 420, 600, QtCore.Qt.IgnoreAspectRatio) + + byte_array = QtCore.QByteArray() + buffer = QtCore.QBuffer(byte_array) + buffer.open(QtCore.QIODevice.WriteOnly) + cover_image.save(buffer, 'jpg', 75) + + cover_image_final = io.BytesIO(byte_array) + cover_image_final.seek(0) + return cover_image_final.getvalue()