Use new ImageMagick API for masthead image generation

This commit is contained in:
Kovid Goyal 2010-08-04 16:09:13 -06:00
parent 6a4148a7a5
commit 0b1f4588ac

View File

@ -1075,46 +1075,22 @@ class BasicNewsRecipe(Recipe):
img.save(open(out_path, 'wb'), 'JPEG') img.save(open(out_path, 'wb'), 'JPEG')
def prepare_masthead_image(self, path_to_image, out_path): def prepare_masthead_image(self, path_to_image, out_path):
import calibre.utils.PythonMagickWand as pw
from ctypes import byref
from calibre import fit_image from calibre import fit_image
from calibre.utils.magick import Image, create_canvas
with pw.ImageMagick(): img = Image()
img = pw.NewMagickWand() img.open(path_to_image)
img2 = pw.NewMagickWand() width, height = img.size
frame = pw.NewMagickWand() scaled, nwidth, nheight = fit_image(width, height, self.MI_WIDTH, self.MI_HEIGHT)
p = pw.NewPixelWand() img2 = create_canvas(width, height)
if img < 0 or img2 < 0 or p < 0 or frame < 0: frame = create_canvas(self.MI_WIDTH, self.MI_HEIGHT)
raise RuntimeError('Out of memory') img2.compose(img)
if not pw.MagickReadImage(img, path_to_image): if scaled:
severity = pw.ExceptionType(0) img2.size = (nwidth, nheight, 'LanczosFilter', 0.5)
msg = pw.MagickGetException(img, byref(severity)) left = int((self.MI_WIDTH - nwidth)/2.0)
raise IOError('Failed to read image from: %s: %s' top = int((self.MI_HEIGHT - nheight)/2.0)
%(path_to_image, msg)) frame.compose(img2, left, top)
pw.PixelSetColor(p, 'white') frame.save(out_path)
width, height = pw.MagickGetImageWidth(img),pw.MagickGetImageHeight(img)
scaled, nwidth, nheight = fit_image(width, height, self.MI_WIDTH, self.MI_HEIGHT)
if not pw.MagickNewImage(img2, width, height, p):
raise RuntimeError('Out of memory')
if not pw.MagickNewImage(frame, self.MI_WIDTH, self.MI_HEIGHT, p):
raise RuntimeError('Out of memory')
if not pw.MagickCompositeImage(img2, img, pw.OverCompositeOp, 0, 0):
raise RuntimeError('Out of memory')
if scaled:
if not pw.MagickResizeImage(img2, nwidth, nheight, pw.LanczosFilter,
0.5):
raise RuntimeError('Out of memory')
left = int((self.MI_WIDTH - nwidth)/2.0)
top = int((self.MI_HEIGHT - nheight)/2.0)
if not pw.MagickCompositeImage(frame, img2, pw.OverCompositeOp,
left, top):
raise RuntimeError('Out of memory')
if not pw.MagickWriteImage(frame, out_path):
raise RuntimeError('Failed to save image to %s'%out_path)
pw.DestroyPixelWand(p)
for x in (img, img2, frame):
pw.DestroyMagickWand(x)
def create_opf(self, feeds, dir=None): def create_opf(self, feeds, dir=None):
if dir is None: if dir is None: