Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pack images into epub #194

Closed
Ynjxsjmh opened this issue Oct 9, 2019 · 3 comments
Closed

How to pack images into epub #194

Ynjxsjmh opened this issue Oct 9, 2019 · 3 comments

Comments

@Ynjxsjmh
Copy link

Ynjxsjmh commented Oct 9, 2019

Temporarily, I use book.setcover() to pack images into epub.

    # add cover image
    book.set_cover("image.jpg", open('cover.jpg', 'rb').read())
    book.set_cover("image2.jpg", open('try.jpg', 'rb').read())

After unzipping the epub file, part of it looks like

image

And it works

View image

image

I have gone through all the samples in this repo, but didn't find a similar example. So now I have two questions:

  1. I wonder if there is other way to implement that.
  2. If I try something like
    # add cover image
    book.set_cover("image.jpg", open('cover.jpg', 'rb').read())
    book.set_cover("./images/image2.jpg", open('try.jpg', 'rb').read())
    c2.content='<h1>About this book</h1><p><img src="./images/image2.jpg" alt="try Image"/>'

I won't see images2.jpg, what's the possible reason?

What the epub file looks like after unzipping

image

The images folder contains image2.jpg and I can see those images by accessing about.xhtml but I couldn't see image2.jpg through epub.

Here is my working code if you have images named cover.jpg and try.jpg

View code
# coding=utf-8

from ebooklib import epub


if __name__ == '__main__':
    book = epub.EpubBook()

    # add metadata
    book.set_identifier('sample123456')
    book.set_title('Sample book')
    book.set_language('en')

    book.add_author('Aleksandar Erkalovic')

    # add cover image
    book.set_cover("image.jpg", open('cover.jpg', 'rb').read())
    book.set_cover("image2.jpg", open('try.jpg', 'rb').read())

    # intro chapter
    c1 = epub.EpubHtml(title='Introduction', file_name='intro.xhtml', lang='hr')
    c1.content=u'<html><head></head><body><h1>Introduction</h1><p>Introduction paragraph where i explain what is happening.</p></body></html>'

    # about chapter
    c2 = epub.EpubHtml(title='About this book', file_name='about.xhtml')
    c2.content='<h1>About this book</h1><p><img src="image2.jpg" alt="try Image"/><p>Helou, this is my book! There are many books, but this one is mine.</p><p><img src="image.jpg" alt="Cover Image"/></p>'

    # add chapters to the book
    book.add_item(c1)
    book.add_item(c2)
    
    # create table of contents
    # - add manual link
    # - add section
    # - add auto created links to chapters

    book.toc = (epub.Link('intro.xhtml', 'Introduction', 'intro'),
                (epub.Section('Languages'),
                 (c1, c2))
                )

    # add navigation files
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    # define css style
    style = '''
@namespace epub "http://www.idpf.org/2007/ops";
body {
    font-family: Cambria, Liberation Serif, Bitstream Vera Serif, Georgia, Times, Times New Roman, serif;
}
h2 {
     text-align: left;
     text-transform: uppercase;
     font-weight: 200;     
}
ol {
        list-style-type: none;
}
ol > li:first-child {
        margin-top: 0.3em;
}
nav[epub|type~='toc'] > ol > li > ol  {
    list-style-type:square;
}
nav[epub|type~='toc'] > ol > li > ol > li {
        margin-top: 0.3em;
}
'''

    # add css file
    nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
    book.add_item(nav_css)

    # create spin, add cover page as first page
    book.spine = ['cover', 'nav', c1, c2]

    # create epub file
    epub.write_epub('test.epub', book, {})

That code will throw a warning

Warning (from warnings module):
  File "C:\Program Files\Python36\lib\zipfile.py", line 1349
    return self._open_to_write(zinfo, force_zip64=force_zip64)
UserWarning: Duplicate name: 'EPUB/cover.xhtml'

But that doesn't matter.

@cassc
Copy link

cassc commented Oct 29, 2019

Hi, I managed to add images with the following code:

file_name = unique_jpg_image_title
ei = epub.EpubImage()
ei.file_name = file_name
ei.media_type = 'image/jpeg'
ei.content = binary_file_content
book.add_item(ei)

unique_jpg_image_title should not starts with . otherwise images wouldn't show in some ebook viewers.

I only tried with jpeg images. I guess other image types would also work.

@Ynjxsjmh
Copy link
Author

Ynjxsjmh commented Oct 29, 2019

Thank you!

I also find a related PR here Add an example for embedding images into paragraph

    from ebooklib import epub
    from PIL import Image  # you need pip install Pillow
    import io

    book = epub.EpubBook()

    # create chapter
    c1 = epub.EpubHtml(title='Intro', file_name='chap_01.xhtml', lang='hr')
    c1.content=u'<h1>Intro heading</h1><p>Zaba je skocila u baru.</p><p><img alt="image1" src="images/image1.jpeg"/><br/></p>'

    # add chapter
    book.add_item(c1)

    # add CSS file
    book.add_item(nav_css)

    # load Image file
    img1 = Image.open('image1.jpeg')  # 'image1.jpeg' should locate in current directory for this example
    b = io.BytesIO()
    img1.save(b, 'jpeg')
    b_image1 = b.getvalue()

    # define Image file path in .epub
    image1_item = epub.EpubItem(uid='image_1', file_name='images/image1.jpeg', media_type='image/jpeg', content=b_image1)

    # add Image file
    book.add_item(image1_item)

    # basic spine
    book.spine = ['nav', c1]

@jeffheaton
Copy link

Suggest merging this! I am currently wasting time looking at issues and other discussion to see exactly how to do this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants