From fdd3968f302195a1f5b0f9e3445a84df009014f2 Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Tue, 4 Apr 2023 11:12:37 +0200
Subject: [PATCH] Reduce image size to avoid error on small data

---
 pyVcardToQr/pyVcardToQr.py | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/pyVcardToQr/pyVcardToQr.py b/pyVcardToQr/pyVcardToQr.py
index 5513170..7ce8d16 100644
--- a/pyVcardToQr/pyVcardToQr.py
+++ b/pyVcardToQr/pyVcardToQr.py
@@ -22,16 +22,6 @@ def getCard(first, last, data):
 
 
 def genQr(data, output, colors=('black', 'white'), logo_path=None):
-    if logo_path is not None:
-        logo = Image.open(logo_path)
-        # taking base width
-        basewidth = 100
-
-        # adjust image size
-        wpercent = (basewidth/float(logo.size[0]))
-        hsize = int((float(logo.size[1])*float(wpercent)))
-        logo = logo.resize((basewidth, hsize), Image.ANTIALIAS)
-
     QRcode = qrcode.QRCode()
 
     # adding URL or text to QRcode
@@ -45,6 +35,14 @@ def genQr(data, output, colors=('black', 'white'), logo_path=None):
     img = QRcode.make_image(fill_color=fill, back_color=back).convert('RGB')
 
     if logo_path is not None:
+        logo = Image.open(logo_path)
+        # taking base width
+        basewidth = min(int(10 * img.width / 100), 100)
+
+        # adjust image size
+        wpercent = (basewidth/float(logo.size[0]))
+        hsize = int((float(logo.size[1])*float(wpercent)))
+        logo = logo.resize((basewidth, hsize), Image.ANTIALIAS)
         # set size of QR code
         pos = ((img.size[0] - logo.size[0]) // 2,
                (img.size[1] - logo.size[1]) // 2)
-- 
GitLab