Quote:
Originally Posted by CalibUser
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.)