+ th_format, ext, media_type = {
+ 'GIF': ('GIF', 'gif', 'image/gif'),
+ 'PNG': ('PNG', 'png', 'image/png'),
+ }.get(img.format, ('JPEG', 'jpg', 'image/jpeg'))
+
+ width = 1200
+ if img.size[0] < width:
+ th = img
+ else:
+ th = img.resize((width, round(width * img.size[1] / img.size[0])))
+
+ imgfile.close()
+
+ buffer = six.BytesIO()
+ th.save(buffer, format=th_format)
+
+ file_name = 'image%d.%s' % (i, ext)
+ ilustr.set('src', file_name)
+ output.add_item(
+ epub.EpubItem(
+ uid='image%s' % i,
+ file_name=file_name,
+ media_type=media_type,
+ content=buffer.getvalue()
+ )
+ )
+