mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Sync to Pluginize
This commit is contained in:
commit
27de18ebc5
@ -10,7 +10,6 @@ from calibre import StreamReadWrapper
|
|||||||
from calibre.ebooks.metadata import MetaInformation, authors_to_string
|
from calibre.ebooks.metadata import MetaInformation, authors_to_string
|
||||||
from calibre.ptempfile import TemporaryDirectory
|
from calibre.ptempfile import TemporaryDirectory
|
||||||
from pyPdf import PdfFileReader, PdfFileWriter
|
from pyPdf import PdfFileReader, PdfFileWriter
|
||||||
import Image
|
|
||||||
try:
|
try:
|
||||||
from calibre.utils.PythonMagickWand import \
|
from calibre.utils.PythonMagickWand import \
|
||||||
NewMagickWand, MagickReadImage, MagickSetImageFormat, \
|
NewMagickWand, MagickReadImage, MagickSetImageFormat, \
|
||||||
@ -95,42 +94,27 @@ def set_metadata(stream, mi):
|
|||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
|
|
||||||
def get_cover(stream):
|
def get_cover(stream):
|
||||||
data = cStringIO.StringIO()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with StreamReadWrapper(stream) as stream:
|
with StreamReadWrapper(stream) as stream:
|
||||||
pdf = PdfFileReader(stream)
|
pdf = PdfFileReader(stream)
|
||||||
output = PdfFileWriter()
|
output = PdfFileWriter()
|
||||||
|
|
||||||
# We only need the first page of the pdf file as that will
|
|
||||||
# be used as the cover. Saving the first page into a new
|
|
||||||
# pdf will speed up processing with ImageMagick as it will
|
|
||||||
# try to create an image for every page in the document.
|
|
||||||
if len(pdf.pages) >= 1:
|
if len(pdf.pages) >= 1:
|
||||||
output.addPage(pdf.getPage(0))
|
output.addPage(pdf.getPage(0))
|
||||||
|
|
||||||
# ImageMagick will only take a file path and save the
|
|
||||||
# image to a file path.
|
|
||||||
with TemporaryDirectory('_pdfmeta') as tdir:
|
with TemporaryDirectory('_pdfmeta') as tdir:
|
||||||
cover_path = os.path.join(tdir, 'cover.pdf')
|
cover_path = os.path.join(tdir, 'cover.pdf')
|
||||||
|
|
||||||
with open(cover_path, "wb") as outputStream:
|
with open(cover_path, "wb") as outputStream:
|
||||||
output.write(outputStream)
|
output.write(outputStream)
|
||||||
|
|
||||||
# Use ImageMagick to turn the pdf into a Jpg image.
|
|
||||||
with ImageMagick():
|
with ImageMagick():
|
||||||
wand = NewMagickWand()
|
wand = NewMagickWand()
|
||||||
MagickReadImage(wand, cover_path)
|
MagickReadImage(wand, cover_path)
|
||||||
MagickSetImageFormat(wand, 'JPEG')
|
MagickSetImageFormat(wand, 'JPEG')
|
||||||
MagickWriteImage(wand, '%s.jpg' % cover_path)
|
MagickWriteImage(wand, '%s.jpg' % cover_path)
|
||||||
|
return open('%s.jpg' % cover_path, 'rb').read()
|
||||||
# We need the image as a stream so we can return the
|
|
||||||
# image as a string for use in a MetaInformation object.
|
|
||||||
img = Image.open('%s.jpg' % cover_path)
|
|
||||||
img.save(data, 'JPEG')
|
|
||||||
except:
|
except:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
# Return the string in the cStringIO object.
|
return ''
|
||||||
return data.getvalue()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user