Improve cover creation for PDFs
This commit is contained in:
@@ -18,11 +18,10 @@
|
|||||||
# Error handling
|
# Error handling
|
||||||
# TOC parsing
|
# TOC parsing
|
||||||
|
|
||||||
import io
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import fitz
|
import fitz
|
||||||
from PyQt5 import QtCore, QtGui
|
from PyQt5 import QtGui
|
||||||
|
|
||||||
|
|
||||||
class ParsePDF:
|
class ParsePDF:
|
||||||
@@ -58,22 +57,13 @@ class ParsePDF:
|
|||||||
return year
|
return year
|
||||||
|
|
||||||
def get_cover_image(self):
|
def get_cover_image(self):
|
||||||
# TODO
|
# This is a little roundabout for the cover
|
||||||
# See if there's any way to stop this roundabout way of
|
# and I'm sure it's taking a performance hit
|
||||||
# getting a smaller QImage from a larger Pixmap
|
# But it is simple. So there's that.
|
||||||
cover_page = self.book.loadPage(0)
|
cover_page = self.book.loadPage(0)
|
||||||
coverPixmap = cover_page.getPixmap()
|
|
||||||
imageFormat = QtGui.QImage.Format_RGB888
|
|
||||||
if coverPixmap.alpha:
|
|
||||||
imageFormat = QtGui.QImage.Format_RGBA8888
|
|
||||||
coverQImage = QtGui.QImage(
|
|
||||||
coverPixmap.samples,
|
|
||||||
coverPixmap.width,
|
|
||||||
coverPixmap.height,
|
|
||||||
coverPixmap.stride,
|
|
||||||
imageFormat)
|
|
||||||
|
|
||||||
return resize_image(coverQImage)
|
# Disabling scaling gets the covers much faster
|
||||||
|
return render_pdf_page(cover_page, True)
|
||||||
|
|
||||||
def get_isbn(self):
|
def get_isbn(self):
|
||||||
return None
|
return None
|
||||||
@@ -90,6 +80,7 @@ class ParsePDF:
|
|||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
# Better parsing of TOC
|
# Better parsing of TOC
|
||||||
|
# file_settings = {'images_only': True}
|
||||||
# contents = self.book.getToC()
|
# contents = self.book.getToC()
|
||||||
# if not contents:
|
# if not contents:
|
||||||
# contents = [
|
# contents = [
|
||||||
@@ -102,42 +93,30 @@ class ParsePDF:
|
|||||||
return contents, file_settings
|
return contents, file_settings
|
||||||
|
|
||||||
|
|
||||||
def resize_image(cover_image):
|
def render_pdf_page(page_data, for_cover=False):
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
def render_pdf_page(page_data):
|
|
||||||
# Draw page contents on to a pixmap
|
# Draw page contents on to a pixmap
|
||||||
pixmap = QtGui.QPixmap()
|
# and then return that pixmap
|
||||||
zoom_matrix = fitz.Matrix(4, 4) # Sets render quality
|
|
||||||
|
# Render quality is set by the following
|
||||||
|
zoom_matrix = fitz.Matrix(4, 4)
|
||||||
|
if for_cover:
|
||||||
|
zoom_matrix = fitz.Matrix(1, 1)
|
||||||
|
|
||||||
pagePixmap = page_data.getPixmap(
|
pagePixmap = page_data.getPixmap(
|
||||||
matrix=zoom_matrix)
|
matrix=zoom_matrix,
|
||||||
imageFormat = QtGui.QImage.Format_RGB888
|
alpha=False) # Sets background to White
|
||||||
if pagePixmap.alpha:
|
imageFormat = QtGui.QImage.Format_RGB888 # Set to Format_RGB888 if alpha
|
||||||
imageFormat = QtGui.QImage.Format_RGBA8888
|
|
||||||
pageQImage = QtGui.QImage(
|
pageQImage = QtGui.QImage(
|
||||||
pagePixmap.samples,
|
pagePixmap.samples,
|
||||||
pagePixmap.width,
|
pagePixmap.width,
|
||||||
pagePixmap.height,
|
pagePixmap.height,
|
||||||
pagePixmap.stride,
|
pagePixmap.stride,
|
||||||
imageFormat)
|
imageFormat)
|
||||||
|
|
||||||
|
# The cover page doesn't require conversion into a Pixmap
|
||||||
|
if for_cover:
|
||||||
|
return pageQImage
|
||||||
|
|
||||||
|
pixmap = QtGui.QPixmap()
|
||||||
pixmap.convertFromImage(pageQImage)
|
pixmap.convertFromImage(pageQImage)
|
||||||
|
return pixmap
|
||||||
# Draw page background
|
|
||||||
# Currently going with White - any color should be possible
|
|
||||||
finalPixmap = QtGui.QPixmap(pixmap.size())
|
|
||||||
finalPixmap.fill(QtGui.QColor(QtCore.Qt.white))
|
|
||||||
imagePainter = QtGui.QPainter(finalPixmap)
|
|
||||||
imagePainter.drawPixmap(0, 0, pixmap)
|
|
||||||
|
|
||||||
return finalPixmap
|
|
||||||
|
@@ -344,8 +344,14 @@ def progress_object_generator():
|
|||||||
|
|
||||||
|
|
||||||
def resize_image(cover_image_raw):
|
def resize_image(cover_image_raw):
|
||||||
cover_image = QtGui.QImage()
|
if isinstance(cover_image_raw, QtGui.QImage):
|
||||||
cover_image.loadFromData(cover_image_raw)
|
cover_image = cover_image_raw
|
||||||
|
else:
|
||||||
|
cover_image = QtGui.QImage()
|
||||||
|
cover_image.loadFromData(cover_image_raw)
|
||||||
|
|
||||||
|
# Resize image to what literally everyone
|
||||||
|
# agrees is an acceptable cover size
|
||||||
cover_image = cover_image.scaled(
|
cover_image = cover_image.scaled(
|
||||||
420, 600, QtCore.Qt.IgnoreAspectRatio)
|
420, 600, QtCore.Qt.IgnoreAspectRatio)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user