1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 
   6 from urllib.request import urlopen
 
   8 from .base import Widget
 
  11 class Background(Widget):
 
  14     def __init__(self, cover, crop_to_square=True):
 
  15         self.crop_to_square = crop_to_square
 
  16         super().__init__(cover)
 
  20         if self.cover.book_info.cover_url:
 
  23                     data = io.BytesIO(urlopen(self.cover.book_info.cover_url, timeout=3).read())
 
  29             img = PIL.Image.open(data)
 
  31             if self.crop_to_square:
 
  33                 if img.size[1] > img.size[0]:
 
  34                     img = img.crop((0, 0, img.size[0], img.size[0]))
 
  36                     left = round((img.size[0] - img.size[1])/2)
 
  45     def build(self, w, h):
 
  53         img = self.img.resize((
 
  54             round(scale * img.size[0]),
 
  55             round(scale * img.size[1]),
 
  58             int((img.size[0] - w) / 2),
 
  60             w + int((img.size[0] - w) / 2),