#!/usr/bin/env python3 import os import zipfile from PIL import Image import sys from io import BytesIO def extract_cover(epub, output_path, size): for fileinfo in epub.filelist: if fileinfo.filename.lower().endswith(('.jpg', '.jpeg', '.png')): cover = epub.open(fileinfo.filename) im = Image.open(BytesIO(cover.read())) im.thumbnail((size, size), Image.LANCZOS) im.save(os.path.join(output_path, os.path.basename(input_file) + '.png'), "PNG") return True return False if __name__ == '__main__': input_file = sys.argv[1] output_path = os.environ.get('THUMBNAIL_DIR', os.path.dirname(input_file)) size = int(sys.argv[2]) epub = zipfile.ZipFile(input_file, "r") if extract_cover(epub, output_path, size): exit(0) else: print("Error extracting cover") exit(1)