Skip to content

Instantly share code, notes, and snippets.

@gabalese
Created September 11, 2013 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabalese/6525015 to your computer and use it in GitHub Desktop.
Save gabalese/6525015 to your computer and use it in GitHub Desktop.
Basic example of how simple it is to add (slightly) less obvious watermarks to EPUB files with pyepub library and stepic
#! /usr/bin/env python
import os
from pyepub import EPUB # see https://github.com/gabalese/pyepub
from StringIO import StringIO
from PIL import Image
import stepic
epub = EPUB("startepub.epub","a")
epub_images = [x["href"] for x in epub.info["manifest"] if x["mimetype"] == "image/jpeg"]
container_img = os.path.join(epub.root_folder, epub_images[0])
flo = StringIO(epub.read(container_img))
epub._delete(container_img)
img = Image.open(flo)
im2 = stepic.encode(img, 'Hi, this is a plain text string! You can encode me or whatever')
flo_out = StringIO()
im2.save(flo_out, format="jpeg")
epub.writestr(container_img, flo_out.getvalue())
epub.writetodisk("new.epub")
@timbroder
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment