View Single Post
Old 09-18-2015, 03:21 PM   #53
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,744
Karma: 24031403
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by CalibUser View Post
Thanks, Doitsu, this method allows me to read an image from an epub into img. After resizing the image, how can I write it back in a format that bk.writefile() can use? I've tried a few approaches including bkimage = BytesIO(self.img ) but without success.
AFAIK, you'll have to use BytesIO.

Code:
from PIL import Image
from io import BytesIO

def run(bk):
    data = bk.readfile('cover.jpg')
    img = Image.open(BytesIO(data))
    img.thumbnail((330, 330), Image.ANTIALIAS)
    imagedata = BytesIO()
    img.save(imagedata, 'png')
    thumbnail = imagedata.getvalue()
    bk.addfile('thumbnail.png', 'thumbnail.png', thumbnail, 'image/png')
(For some odd reason img.save() doesn't appear to work with jpg as the file format.)

Last edited by Doitsu; 09-18-2015 at 03:50 PM. Reason: Code updated for Python 3.4
Doitsu is offline   Reply With Quote