summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/controller.py
diff options
context:
space:
mode:
Diffstat (limited to 'indoteknik_api/controllers/controller.py')
-rw-r--r--indoteknik_api/controllers/controller.py22
1 files changed, 18 insertions, 4 deletions
diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py
index 7ceebbe3..bd763e00 100644
--- a/indoteknik_api/controllers/controller.py
+++ b/indoteknik_api/controllers/controller.py
@@ -194,7 +194,8 @@ class Controller(http.Controller):
model_with_watermark = ['product.template', 'product.product']
watermark = kw.get('watermark', '')
if watermark.lower() == 'true' or model_name in model_with_watermark:
- image = self.add_watermark_to_image(image)
+ ratio = kw.get('ratio', '')
+ image = self.add_watermark_to_image(image, ratio)
response_headers = [
('Content-Type', 'image/jpg'),
@@ -206,7 +207,7 @@ class Controller(http.Controller):
response_headers
)
- def add_watermark_to_image(self, image):
+ def add_watermark_to_image(self, image, ratio):
logo_path = get_module_resource('indoteknik_api', 'static', 'src', 'images', 'logo-indoteknik.png')
logo_img = Image.open(logo_path)
logo_img = logo_img.convert('RGBA')
@@ -217,10 +218,23 @@ class Controller(http.Controller):
img_width, img_height = img.size
logo_img.thumbnail((img_width // 2.2, img_height // 2.2))
+
+ new_img = img
+
+ if ratio == 'square':
+ longest_wh = max(img_height, img_width)
- img.paste(logo_img, (12, 10), logo_img)
+ new_img = Image.new('RGBA', (longest_wh, longest_wh), (255, 255, 255, 255))
+
+ paste_x = (longest_wh - img_width) // 2
+ paste_y = (longest_wh - img_height) // 2
+
+ new_img.paste(img, (paste_x, paste_y), img)
+
+ # Add watermark
+ new_img.paste(logo_img, (12, 10), logo_img)
buffered = io.BytesIO()
- img.save(buffered, format="PNG")
+ new_img.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode('utf-8')