Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update textsize() method to textbbox() #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Instructions

- `pip install qrcode[pil]`
- If using a Debian-based Linux operating systems: `sudo apt install ttf-mscorefonts-installer`
- Execute `python generator.py`
- Follow instructions
8 changes: 4 additions & 4 deletions generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ def parse_objects(data):
font = ImageFont.truetype("Arial.ttf", 30)

# Calculate text size and position
text_size = draw.textsize(c, font)
if text_size[0] > img.size[0]:
_, _, text_width, text_height = draw.textbbox((0, 0), c, font=font)
if text_width > img.size[0]:
font = ImageFont.truetype("Arial.ttf", 15)
text_size = draw.textsize(c, font)
text_position = ((img.size[0] - text_size[0]) // 2, img.size[1] - text_size[1] - 10)
_, _, text_width, text_height = draw.textbbox((0, 0), c, font=font)
text_position = ((img.size[0] - text_width) // 2, img.size[1] - text_height - 10)

# Draw text on the image
draw.text(text_position, c, font=font, fill="black")
Expand Down