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 PIL import ImageOps
 
   9 from .base import Widget
 
  12 class Background(Widget):
 
  15     def __init__(self, cover, crop_to_square=True):
 
  16         self.crop_to_square = crop_to_square
 
  17         super().__init__(cover)
 
  21         if self.cover.book_info.cover_url:
 
  24                     data = io.BytesIO(urlopen(self.cover.book_info.cover_url, timeout=3).read())
 
  30             img = PIL.Image.open(data)
 
  31             img = ImageOps.exif_transpose(img)
 
  33             if self.crop_to_square:
 
  35                 if img.size[1] > img.size[0]:
 
  36                     img = img.crop((0, 0, img.size[0], img.size[0]))
 
  38                     left = round((img.size[0] - img.size[1])/2)
 
  47     def build(self, w, h):
 
  55         img = self.img.resize((
 
  56             round(scale * img.size[0]),
 
  57             round(scale * img.size[1]),
 
  60             int((img.size[0] - w) / 2),
 
  62             w + int((img.size[0] - w) / 2),