View Single Post
Old 05-29-2018, 07:53 AM   #35
jtheelen
Junior Member
jtheelen began at the beginning.
 
Posts: 1
Karma: 10
Join Date: May 2018
Device: Sony eReader PRS-T3
Here's a Python 2.7 script to create a large poster with all the covers of your e-book files in Calibre (requires libraries PIL and BeautifulSoup):

Spoiler:
Code:
from bs4 import BeautifulSoup
from PIL import Image
import fnmatch
import os


PATH_TO_CALIBRE_LIBRARY = './Calibre Library'
PATH_TO_POSTER = './poster.jpg'

COLS = 10  # book covers per row
WIDTH = 1000  # px

KEEP_TAGS = None
SKIP_TAGS = set(['foo', 'bar'])  # skip books with Calibre tags 'foo' or 'bar'

books = []
for book, _, filenames in os.walk(PATH_TO_CALIBRE_LIBRARY):
    for _ in fnmatch.filter(filenames, 'cover.jpg'):
        if not KEEP_TAGS and not SKIP_TAGS:
            books.append(book)
        else:
            soup = BeautifulSoup(open(os.path.join(book, 'metadata.opf')), 'lxml')
            tags = set(str(tag.text) for tag in soup.findAll('dc:subject', text=True))
            if KEEP_TAGS:
                if KEEP_TAGS & tags:
                    books.append(book)
            elif SKIP_TAGS:
                if not SKIP_TAGS & tags:
                    books.append(book)

cover_height = []
for book in books:
    w, h = Image.open(os.path.join(book, 'cover.jpg')).size
    cover_height.append((int(h * WIDTH / w), book))

cover_height = sorted(cover_height)

poster_height = sum(h for h, _ in cover_height[-len(books) / COLS - 1:])
poster = Image.new('RGB', (COLS * WIDTH, poster_height), (255, 255, 255))

row_height = {}
for i, (h, book) in enumerate(cover_height):
    cover = os.path.join(book, 'cover.jpg')
    row_height[i / COLS] = h
    poster.paste(Image.open(cover).resize((WIDTH, h), Image.ANTIALIAS),
                 ((i % COLS) * WIDTH, sum(list(row_height.values())[:-1])))

poster.crop((0, 0, COLS * WIDTH, sum(row_height.values()))).save(PATH_TO_POSTER)

print('{} book covers added in {} columns and {} rows'
      .format(len(books), COLS, len(row_height)))

Last edited by jtheelen; 06-16-2018 at 02:21 PM.
jtheelen is offline   Reply With Quote