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')
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.utils.magick import Image, create_canvas
with pw.ImageMagick():
img = pw.NewMagickWand()
img2 = pw.NewMagickWand()
frame = pw.NewMagickWand()
p = pw.NewPixelWand()
if img < 0 or img2 < 0 or p < 0 or frame < 0:
raise RuntimeError('Out of memory')
if not pw.MagickReadImage(img, path_to_image):
severity = pw.ExceptionType(0)
msg = pw.MagickGetException(img, byref(severity))
raise IOError('Failed to read image from: %s: %s'
%(path_to_image, msg))
pw.PixelSetColor(p, 'white')
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)
img = Image()
img.open(path_to_image)
width, height = img.size
scaled, nwidth, nheight = fit_image(width, height, self.MI_WIDTH, self.MI_HEIGHT)
img2 = create_canvas(width, height)
frame = create_canvas(self.MI_WIDTH, self.MI_HEIGHT)
img2.compose(img)
if scaled:
img2.size = (nwidth, nheight, 'LanczosFilter', 0.5)
left = int((self.MI_WIDTH - nwidth)/2.0)
top = int((self.MI_HEIGHT - nheight)/2.0)
frame.compose(img2, left, top)
frame.save(out_path)
def create_opf(self, feeds, dir=None):
if dir is None: