3 from urllib.request import urlopen
5 from .base import Widget
8 class Background(Widget):
11 def __init__(self, cover, crop_to_square=True):
12 self.crop_to_square = crop_to_square
13 super().__init__(cover)
17 if self.cover.book_info.cover_url:
20 data = io.BytesIO(urlopen(self.cover.book_info.cover_url, timeout=3).read())
26 img = PIL.Image.open(data)
28 if self.crop_to_square:
30 if img.size[1] > img.size[0]:
31 img = img.crop((0, 0, img.size[0], img.size[0]))
33 left = round((img.size[0] - img.size[1])/2)
42 def build(self, w, h):
50 img = self.img.resize((
51 round(scale * img.size[0]),
52 round(scale * img.size[1]),
55 int((img.size[0] - w) / 2),
57 w + int((img.size[0] - w) / 2),