Skip to content

Commit 0e88ea4

Browse files
committed
Automatic commit Mon Jan 18 12:59:43 PM EET 2021
1 parent a9767b6 commit 0e88ea4

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

epub/epub-show-cover.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
# Author: Antonios Tsolis (Alamot)
3+
import os
4+
import sys
5+
import zipfile
6+
from lxml import etree
7+
from PIL import Image
8+
9+
10+
# Let's define the required XML namespaces
11+
namespaces = {
12+
"calibre":"http://calibre.kovidgoyal.net/2009/metadata",
13+
"dc":"http://purl.org/dc/elements/1.1/",
14+
"dcterms":"http://purl.org/dc/terms/",
15+
"opf":"http://www.idpf.org/2007/opf",
16+
"u":"urn:oasis:names:tc:opendocument:xmlns:container",
17+
"xsi":"http://www.w3.org/2001/XMLSchema-instance",
18+
}
19+
20+
21+
def get_epub_cover(epub_path):
22+
''' Return the cover image file from an epub archive. '''
23+
24+
# We open the epub archive using zipfile.ZipFile():
25+
with zipfile.ZipFile(epub_path) as z:
26+
27+
# We load "META-INF/container.xml" using lxml.etree.fromString():
28+
t = etree.fromstring(z.read("META-INF/container.xml"))
29+
# We use xpath() to find the attribute "full-path":
30+
'''
31+
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
32+
<rootfiles>
33+
<rootfile full-path="OEBPS/content.opf" ... />
34+
</rootfiles>
35+
</container>
36+
'''
37+
rootfile_path = t.xpath("/u:container/u:rootfiles/u:rootfile",
38+
namespaces=namespaces)[0].get("full-path")
39+
print("Path of root file found: " + rootfile_path)
40+
41+
# We load the "root" file, indicated by the "full_path" attribute of "META-INF/container.xml", using lxml.etree.fromString():
42+
t = etree.fromstring(z.read(rootfile_path))
43+
# We use xpath() to find the attribute "content":
44+
'''
45+
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
46+
...
47+
<meta content="my-cover-image" name="cover"/>
48+
...
49+
</metadata>
50+
'''
51+
cover_id = t.xpath("//opf:metadata/opf:meta[@name='cover']",
52+
namespaces=namespaces)[0].get("content")
53+
print("ID of cover image found: " + cover_id)
54+
55+
# We use xpath() to find the attribute "href":
56+
'''
57+
<manifest>
58+
...
59+
<item id="my-cover-image" href="images/978.jpg" ... />
60+
...
61+
</manifest>
62+
'''
63+
cover_href = t.xpath("//opf:manifest/opf:item[@id='" + cover_id + "']",
64+
namespaces=namespaces)[0].get("href")
65+
# In order to get the full path for the cover image, we have to join rootfile_path and cover_href:
66+
cover_path = os.path.join(os.path.dirname(rootfile_path), cover_href)
67+
print("Path of cover image found: " + cover_path)
68+
69+
# We return the image
70+
return z.open(cover_path)
71+
72+
73+
if len(sys.argv) < 2:
74+
print("Usage: " + sys.argv[0] + " filename.epub")
75+
exit()
76+
77+
epubfile = sys.argv[1]
78+
if not os.path.isfile(epubfile):
79+
print("File not found: " + epubfile)
80+
exit()
81+
82+
image = Image.open(get_epub_cover(epubfile))
83+
image.show()
84+
85+

0 commit comments

Comments
 (0)