'Convert iTunes artwork to thumbnail
Cache generated thumbnails
cache_dir = os.path.join(config_dir, \'caches\', \'itunes\')
as of iTunes 9.2, iBooks 1.1, can\'t set artwork for PDF files via automation'

def _generate_thumbnail(self, book_path, book):

    from PIL import Image as PILImage
    from calibre.utils.zipfile import ZipFile
    if (not self.settings().extra_customization[self.CACHE_COVERS]):
        thumb_data = None
        return thumb_data
    thumb_path = (book_path.rpartition('.')[0] + '.jpg')
    if isosx:
        title = book.name()
    elif iswindows:
        title = book.Name
    try:
        zfr = ZipFile(self.archive_path)
        thumb_data = zfr.read(thumb_path)
        if (thumb_data == 'None'):
            if False:
                logger().info(("    %s._generate_thumbnail()\n            returning    None    from    cover    cache    for    '%s'" % (self.__class__.__name__, title)))
            zfr.close()
            return None
    except:
        zfw = ZipFile(self.archive_path, mode='a')
    else:
        if False:
            logger().info(("            returning    thumb    from    cache    for    '%s'" % title))
        return thumb_data
    if self.verbose:
        logger().info(("    %s._generate_thumbnail('%s'):" % (self.__class__.__name__, title)))
    if isosx:
        try:
            data = book.artworks[1].raw_data().data
        except:
            if self.verbose:
                logger().error(("        error    fetching    iTunes    artwork    for    '%s'" % title))
            zfw.writestr(thumb_path, 'None')
            zfw.close()
            return None
        try:
            img_data = cStringIO.StringIO(data)
            im = PILImage.open(img_data)
            (scaled, width, height) = fit_image(im.size[0], im.size[1], 60, 80)
            im = im.resize((int(width), int(height)), PILImage.ANTIALIAS)
            thumb = cStringIO.StringIO()
            im.convert('RGB').save(thumb, 'JPEG')
            thumb_data = thumb.getvalue()
            thumb.close()
            if False:
                logger().info(("        generated    thumb    for    '%s',    caching" % title))
            zfw.writestr(thumb_path, thumb_data)
        except:
            if self.verbose:
                logger().info(("        ERROR:    error    generating    thumb    for    '%s',    caching    empty    marker" % book.name()))
                self._dump_hex(data[:32])
            thumb_data = None
            zfw.writestr(thumb_path, 'None')
        finally:
            img_data.close()
            zfw.close()
        return thumb_data
    elif iswindows:
        if (not book.Artwork.Count):
            if self.verbose:
                logger().info(("        no    artwork    available    for    '%s'" % book.Name))
            zfw.writestr(thumb_path, 'None')
            zfw.close()
            return None
        try:
            tmp_thumb = os.path.join(tempfile.gettempdir(), ('thumb.%s' % self.ArtworkFormat[book.Artwork.Item(1).Format]))
            book.Artwork.Item(1).SaveArtworkToFile(tmp_thumb)
            im = PILImage.open(tmp_thumb)
            (scaled, width, height) = fit_image(im.size[0], im.size[1], 60, 80)
            im = im.resize((int(width), int(height)), PILImage.ANTIALIAS)
            thumb = cStringIO.StringIO()
            im.convert('RGB').save(thumb, 'JPEG')
            thumb_data = thumb.getvalue()
            os.remove(tmp_thumb)
            thumb.close()
            if False:
                logger().info(("        generated    thumb    for    '%s',    caching" % book.Name))
            zfw.writestr(thumb_path, thumb_data)
        except:
            if self.verbose:
                logger().error(("        error    generating    thumb    for    '%s',    caching    empty    marker" % book.Name))
            thumb_data = None
            zfw.writestr(thumb_path, 'None')
        finally:
            zfw.close()
        return thumb_data
