def add_illustrations(self): src_illus_fpath = self.build_dir / "illustration" # if user provided a custom favicon, retrieve that if not self.conf.favicon: self.conf.favicon = Global.site["BadgeIconUrl"] handle_user_provided_file(source=self.conf.favicon, dest=src_illus_fpath) # convert to PNG (might already be PNG but it's OK) illus_fpath = src_illus_fpath.with_suffix(".png") convert_image(src_illus_fpath, illus_fpath) # resize to appropriate size (ZIM uses 48x48 so we double for retina) for size in (96, 48): resize_image(illus_fpath, width=size, height=size, method="thumbnail") with open(illus_fpath, "rb") as fh: Global.creator.add_illustration(size, fh.read()) # download and add actual favicon (ICO file) favicon_fpath = self.build_dir / "favicon.ico" handle_user_provided_file(source=Global.site["IconUrl"], dest=favicon_fpath) Global.creator.add_item_for("favicon.ico", fpath=favicon_fpath, is_front=False) # download apple-touch-icon Global.creator.add_item( URLItem(url=Global.site["BadgeIconUrl"], path="apple-touch-icon.png"))
def test_change_image_format(png_image, jpg_image, tmp_path, src_fmt, dst_fmt, colorspace): src, _ = get_src_dst(tmp_path, src_fmt, png_image=png_image, jpg_image=jpg_image) dst = tmp_path / f"out.{dst_fmt.lower()}" convert_image(src, dst, fmt=dst_fmt, colorspace=colorspace) dst_image = Image.open(dst) if colorspace: assert dst_image.mode == colorspace assert dst_image.format == dst_fmt
def test_change_image_format_defaults(png_image, jpg_image, tmp_path): # PNG to JPEG (loosing alpha) dst = tmp_path.joinpath(f"{png_image.stem}.jpg") convert_image(png_image, dst) dst_image = Image.open(dst) assert dst_image.mode == "RGB" assert dst_image.format == "JPEG" # PNG to WebP (keeping alpha) dst = tmp_path.joinpath(f"{png_image.stem}.webp") convert_image(png_image, dst) dst_image = Image.open(dst) assert dst_image.mode == "RGBA" assert dst_image.format == "WEBP"
def add_favicon(self): favicon_orig = self.build_dir / "favicon" # if user provided a custom favicon, retrieve that if self.favicon: handle_user_provided_file(source=self.favicon, dest=favicon_orig) # otherwise, get thumbnail from database else: # add channel thumbnail as favicon try: favicon_prefix, favicon_data = self.db.get_channel_metadata( self.channel_id)["thumbnail"].split(";base64,", 1) favicon_data = base64.standard_b64decode(favicon_data) # favicon_mime = favicon_prefix.replace("data:", "") with open(favicon_orig, "wb") as fh: fh.write(favicon_data) del favicon_data except Exception as exc: logger.warning( "Unable to extract favicon from DB; using default") logger.exception(exc) # use a default favicon handle_user_provided_file(source=self.templates_dir / "kolibri-logo.png", dest=favicon_orig) # convert to PNG (might already be PNG but it's OK) favicon_fpath = favicon_orig.with_suffix(".png") convert_image(favicon_orig, favicon_fpath) # resize to appropriate size (ZIM uses 48x48 so we double for retina) for size in (96, 48): resize_image(favicon_fpath, width=size, height=size, method="thumbnail") with open(favicon_fpath, "rb") as fh: self.creator.add_illustration(size, fh.read()) # resize to appropriate size (ZIM uses 48x48) resize_image(favicon_fpath, width=96, height=96, method="thumbnail") # generate favicon favicon_ico_path = favicon_fpath.with_suffix(".ico") create_favicon(src=favicon_fpath, dst=favicon_ico_path) self.creator.add_item_for("favicon.png", fpath=favicon_fpath) self.creator.add_item_for("favicon.ico", fpath=favicon_ico_path)
def add_illustration(self, record=None): if self.favicon_url in self.indexed_urls: return # add illustration from favicon option or in-warc favicon logger.info("Adding illustration from " + (self.favicon_url if record is None else "WARC")) favicon_fname = pathlib.Path(urlparse(self.favicon_url).path).name src_illus_fpath = pathlib.Path(".").joinpath(favicon_fname) # reusing payload from WARC record if record: with open(src_illus_fpath, "wb") as fh: if hasattr(record, "buffered_stream"): record.buffered_stream.seek(0) fh.write(record.buffered_stream.read()) else: fh.write(record.content_stream().read()) # fetching online else: try: handle_user_provided_file(source=self.favicon_url, dest=src_illus_fpath) except Exception as exc: logger.warning( "Unable to retrieve favicon. " "ZIM won't have an illustration: {exc}".format(exc=exc)) return # convert to PNG (might already be PNG but it's OK) illus_fpath = src_illus_fpath.with_suffix(".png") convert_image(src_illus_fpath, illus_fpath) # resize to appropriate size (ZIM uses 48x48 so we double for retina) for size in (96, 48): resize_image(illus_fpath, width=size, height=size, method="thumbnail") with open(illus_fpath, "rb") as fh: self.creator.add_illustration(size, fh.read()) src_illus_fpath.unlink()