def _set_watermark(self, image: TextIO) -> TextIO: check = ['center', 'topleft', 'topright', 'bottomleft', 'bottomright'] if self.watermark not in check: raise ValueError("Param watermark must be between {}".format( ', '.join(check))) imageWidth, imageHeight = image.size watermark = os.path.join(self._BASE_DIR, 'watermark.png') if os.path.exists(watermark): with Image.open(watermark) as logo: logoWidth, logoHeight = logo.size if self.watermark == 'center': image.paste(logo, (int((imageWidth - logoWidth) / 2), int((imageHeight - logoHeight) / 2)), logo) if self.watermark == 'topleft': image.paste(logo, (0, 0), logo) if self.watermark == 'topright': image.paste(logo, (imageWidth - logoWidth, 0), logo) if self.watermark == 'bottomleft': image.paste(logo, (0, imageHeight - logoHeight), logo) if self.watermark == 'bottomright': image.paste( logo, (imageWidth - logoWidth, imageHeight - logoHeight), logo) return image