mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Rename FileWrapper with a more fitting name. Comment pdf get_cover to remove ambiguity of what and why.
This commit is contained in:
parent
b104286f61
commit
fe3d1f5bc7
@ -246,7 +246,7 @@ class CurrentDir(object):
|
|||||||
os.chdir(self.cwd)
|
os.chdir(self.cwd)
|
||||||
|
|
||||||
|
|
||||||
class FileWrapper(object):
|
class StreamReadWrapper(object):
|
||||||
'''
|
'''
|
||||||
Used primarily with pyPdf to ensure the stream is properly closed.
|
Used primarily with pyPdf to ensure the stream is properly closed.
|
||||||
'''
|
'''
|
||||||
|
@ -6,7 +6,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
import sys, os, cStringIO
|
import sys, os, cStringIO
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
from calibre import FileWrapper
|
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
|
||||||
@ -34,7 +34,7 @@ def get_metadata(stream, extract_cover=True):
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with FileWrapper(stream) as stream:
|
with StreamReadWrapper(stream) as stream:
|
||||||
info = PdfFileReader(stream).getDocumentInfo()
|
info = PdfFileReader(stream).getDocumentInfo()
|
||||||
if info.title:
|
if info.title:
|
||||||
mi.title = info.title
|
mi.title = info.title
|
||||||
@ -98,29 +98,39 @@ def get_cover(stream):
|
|||||||
data = cStringIO.StringIO()
|
data = cStringIO.StringIO()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pdf = PdfFileReader(stream)
|
StreamReadWrapper(stream) as stream:
|
||||||
output = PdfFileWriter()
|
pdf = PdfFileReader(stream)
|
||||||
|
output = PdfFileWriter()
|
||||||
|
|
||||||
if len(pdf.pages) >= 1:
|
# We only need the first page of the pdf file as that will
|
||||||
output.addPage(pdf.getPage(0))
|
# 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:
|
||||||
|
output.addPage(pdf.getPage(0))
|
||||||
|
|
||||||
with TemporaryDirectory('_pdfmeta') as tdir:
|
# ImageMagick will only take a file path and save the
|
||||||
cover_path = os.path.join(tdir, 'cover.pdf')
|
# image to a file path.
|
||||||
|
with TemporaryDirectory('_pdfmeta') as tdir:
|
||||||
|
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)
|
||||||
|
|
||||||
with ImageMagick():
|
# Use ImageMagick to turn the pdf into a Jpg image.
|
||||||
wand = NewMagickWand()
|
with ImageMagick():
|
||||||
MagickReadImage(wand, cover_path)
|
wand = NewMagickWand()
|
||||||
MagickSetImageFormat(wand, 'JPEG')
|
MagickReadImage(wand, cover_path)
|
||||||
MagickWriteImage(wand, '%s.jpg' % cover_path)
|
MagickSetImageFormat(wand, 'JPEG')
|
||||||
|
MagickWriteImage(wand, '%s.jpg' % cover_path)
|
||||||
|
|
||||||
|
# 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 = Image.open('%s.jpg' % cover_path)
|
||||||
img.save(data, 'JPEG')
|
img.save(data, 'JPEG')
|
||||||
except:
|
except:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
# Return the string in the cStringIO object.
|
||||||
return data.getvalue()
|
return data.getvalue()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user