Add functionality to the magick_draw module and add commented out code to use it to resize new cover when updating EPUB metadata

This commit is contained in:
Kovid Goyal 2010-07-31 19:29:27 -06:00
parent 983ff06f35
commit 497150207c
3 changed files with 34 additions and 8 deletions

View File

@ -5,7 +5,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
'''Read meta information from epub files'''
import os, re, posixpath
import os, re, posixpath, shutil
from cStringIO import StringIO
from contextlib import closing
@ -13,7 +13,7 @@ from calibre.utils.zipfile import ZipFile, BadZipfile, safe_replace
from calibre.ebooks.BeautifulSoup import BeautifulStoneSoup
from calibre.ebooks.metadata import MetaInformation
from calibre.ebooks.metadata.opf2 import OPF
from calibre.ptempfile import TemporaryDirectory
from calibre.ptempfile import TemporaryDirectory, PersistentTemporaryFile
from calibre import CurrentDir
class EPubException(Exception):
@ -205,11 +205,19 @@ def set_metadata(stream, mi, apply_null=False, update_timestamp=False):
cover_replacable = not reader.encryption_meta.is_encrypted(cpath) and \
os.path.splitext(cpath)[1].lower() in ('.png', '.jpg', '.jpeg')
if cover_replacable:
from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.magick_draw import save_cover_data_to
from calibre.utils.magick_draw import save_cover_data_to, \
identify
new_cover = PersistentTemporaryFile(suffix=os.path.splitext(cpath)[1])
new_cover.close()
save_cover_data_to(new_cdata, new_cover.name)
resize_to = None
if False: # Resize new cover to same size as old cover
shutil.copyfileobj(reader.open(cpath), new_cover)
new_cover.close()
width, height, fmt = identify(new_cover.name)
resize_to = (width, height)
else:
new_cover.close()
save_cover_data_to(new_cdata, new_cover.name,
resize_to=resize_to)
replacements[cpath] = open(new_cover.name, 'rb')
except:
import traceback

View File

@ -2661,7 +2661,7 @@ else:
MagickGetImageSignature = _magick.MagickGetImageSignature
# MagickGetImageFormat
try:
_magick.MagickGetImageFormat.restype = ctypes.POINTER(ctypes.c_char)
_magick.MagickGetImageFormat.restype = ctypes.c_char_p
_magick.MagickGetImageFormat.argtypes = (MagickWand,)
except AttributeError,e:
pass

View File

@ -220,7 +220,7 @@ def create_cover_page(top_lines, logo_path, width=590, height=750,
p.DestroyMagickWand(canvas)
return ans
def save_cover_data_to(data, path, bgcolor='white'):
def save_cover_data_to(data, path, bgcolor='white', resize_to=None):
'''
Saves image in data to path, in the format specified by the path
extension. Composes the image onto a blank canvas so as to
@ -230,6 +230,8 @@ def save_cover_data_to(data, path, bgcolor='white'):
f.write(data)
with p.ImageMagick():
img = load_image(path)
if resize_to is not None:
p.MagickResizeImage(img, resize_to[0], resize_to[1], p.CatromFilter, 1.0)
canvas = create_canvas(p.MagickGetImageWidth(img),
p.MagickGetImageHeight(img), bgcolor)
compose_image(canvas, img, 0, 0)
@ -237,6 +239,22 @@ def save_cover_data_to(data, path, bgcolor='white'):
p.DestroyMagickWand(img)
p.DestroyMagickWand(canvas)
def identify(path):
'''
Identify the image at path. Returns a 3-tuple
(width, height, format)
or raises an IOError.
'''
with p.ImageMagick():
img = load_image(path)
width = p.MagickGetImageWidth(img)
height = p.MagickGetImageHeight(img)
fmt = p.MagickGetImageFormat(img)
if not fmt:
fmt = ''
fmt = fmt.decode('utf-8', 'replace')
return (width, height, fmt)
def test():
import subprocess
with TemporaryFile('.png') as f: