Resize cover images

This commit is contained in:
BasioMeusPuga
2017-11-20 09:10:10 +05:30
parent 155ef22089
commit b28a054c90
2 changed files with 26 additions and 8 deletions

View File

@@ -32,7 +32,6 @@ class Library:
print('Database returned nothing') print('Database returned nothing')
return return
elif mode == 'addition': elif mode == 'addition':
# Assumes parent_window.viewModel already exists and may be extended # Assumes parent_window.viewModel already exists and may be extended
# Because any additional books have already been added to the # Because any additional books have already been added to the
@@ -57,7 +56,6 @@ class Library:
else: else:
return return
for i in books: for i in books:
# The database query returns (or the extension data is) # The database query returns (or the extension data is)
# an iterable with the following indices: # an iterable with the following indices:
@@ -106,7 +104,7 @@ class Library:
img_pixmap.loadFromData(cover) img_pixmap.loadFromData(cover)
else: else:
img_pixmap.load(':/images/NotFound.png') 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 = QtGui.QStandardItem()
item.setToolTip(tooltip_string) item.setToolTip(tooltip_string)
# The following order is needed to keep sorting working # The following order is needed to keep sorting working

View File

@@ -3,11 +3,12 @@
# TODO # TODO
# See if you want to include a hash of the book's name and author # See if you want to include a hash of the book's name and author
import io
import os import os
import pickle import pickle
import hashlib import hashlib
from multiprocessing.dummy import Pool from multiprocessing.dummy import Pool
from PyQt5 import QtCore from PyQt5 import QtCore, QtGui
import database import database
@@ -141,10 +142,13 @@ class BookSorter:
# Different modes require different values # Different modes require different values
if self.mode == 'addition': if self.mode == 'addition':
cover_image = book_ref.get_cover_image() cover_image_raw = book_ref.get_cover_image()
# TODO if cover_image_raw:
# Consider sizing down the image in case # Reduce the size of the incoming image
# it's too big cover_image = resize_image(cover_image_raw)
else:
cover_image = None
self.all_books[file_md5] = { self.all_books[file_md5] = {
'title': title, 'title': title,
'author': author, 'author': author,
@@ -188,3 +192,19 @@ class BookSorter:
_pool.join() _pool.join()
return self.all_books 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()