1
1
import base64
2
2
import qrcode
3
-
3
+ import sys
4
4
from io import BytesIO
5
5
from PIL import Image , ImageFont , ImageDraw
6
- from importlib import resources as importlib_resources
6
+
7
+ if sys .version_info .major == 3 and sys .version_info .minor < 12 :
8
+ from pkg_resources import resource_stream
9
+ else :
10
+ from importlib import resources as importlib_resources
7
11
8
12
def get_qr_with_text (qr , descr ):
9
13
dsi = get_qr_text (qr .size , descr )
@@ -33,7 +37,10 @@ def get_qr_text(max_size, text, font='TahomaBold', font_size=0):
33
37
text_too_large = True
34
38
font_size = 56
35
39
while text_too_large :
36
- file_path = importlib_resources .files (__name__ ).joinpath ('fonts/{}.ttf' .format (font ))
40
+ if sys .version_info .major == 3 and sys .version_info .minor < 12 :
41
+ file_path = resource_stream (__name__ , 'fonts/{}.ttf' .format (font ))
42
+ else :
43
+ file_path = importlib_resources .files (__name__ ).joinpath ('fonts/{}.ttf' .format (font ))
37
44
try :
38
45
fnt = ImageFont .truetype (file_path , font_size )
39
46
except Exception :
@@ -45,7 +52,10 @@ def get_qr_text(max_size, text, font='TahomaBold', font_size=0):
45
52
text_too_large = False
46
53
font_size -= 1
47
54
else :
48
- file_path = importlib_resources .files (__name__ ).joinpath ('fonts/{}.ttf' .format (font ))
55
+ if sys .version_info .major == 3 and sys .version_info .minor < 12 :
56
+ file_path = resource_stream (__name__ , 'fonts/{}.ttf' .format (font ))
57
+ else :
58
+ file_path = importlib_resources .files (__name__ ).joinpath ('fonts/{}.ttf' .format (font ))
49
59
try :
50
60
fnt = ImageFont .truetype (file_path , font_size )
51
61
except Exception :
0 commit comments