From 482c1ab26e1639b94771f69aaafd60f447cfa75b Mon Sep 17 00:00:00 2001 From: David Lynch Date: Mon, 8 Oct 2018 09:02:15 -0500 Subject: [PATCH] Reset cover bytesteam after checking its format Caused issues writing the file later, as the internal pointer moved past the first few bytes when checking whether it was a PNG. Fixes #19 --- ebook/cover.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ebook/cover.py b/ebook/cover.py index 4420d42..743cfb6 100644 --- a/ebook/cover.py +++ b/ebook/cover.py @@ -38,7 +38,12 @@ def make_cover_from_url(url, title, author): img = requests.Session().get(url) cover = BytesIO(img.content) - if Image.open(cover).format != "PNG": + imgformat = Image.open(cover).format + # The `Image.open` read a few bytes from the stream to work out the + # format, so reset it: + cover.seek(0) + + if imgformat != "PNG": cover = _convert_to_png(cover) except Exception as e: logger.info("Encountered an error downloading cover: " + e)