View Single Post
Old 06-21-2010, 01:04 PM   #2179
rford
Junior Member
rford began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Jun 2010
Device: kobo
Thumbs down Rotating Images.

I have a custom recipe to download all my favorite comic strips. Similar to the xkcd.recipe.

The one thing I found annoying was the image in the epub were too wide and were getting cut off. So I rotated them. Now long 3 and 4 panel strips are landscape.

here is the code snippet that I used to rotate the images. Hopefully others will find it useful.
Code:
import calibre.utils.PythonMagickWand as pw
Code:
    def postprocess_html(self, soup, first):
        #process all the images. assumes that the new html has the correct path
        for tag in soup.findAll(lambda tag: tag.name.lower()=='img' and tag.has_key('src')):
            iurl = tag['src']
            print 'resizing image' + iurl
            with pw.ImageMagick():
                img = pw.NewMagickWand()
                p = pw.NewPixelWand()
                if img < 0:
                    raise RuntimeError('Out of memory')
                if not pw.MagickReadImage(img, iurl):
                    severity = pw.ExceptionType(0)
                    msg = pw.MagickGetException(img, byref(severity))
                    raise IOError('Failed to read image from: %s: %s'
                        %(iurl, msg))
                
                width = pw.MagickGetImageWidth(img)
                height = pw.MagickGetImageHeight(img)

                if( width > height ) :
                    print 'Rotate image'
                    pw.MagickRotateImage(img, p, 90)

                if not pw.MagickWriteImage(img, iurl):
                    raise RuntimeError('Failed to save image to %s'%iurl)
                pw.DestroyMagickWand(img)


        return soup
rford is offline