I would like to create a PNG using Pillow. The PNG is later used for label printing. So, I try to create an new image using PIL, add some text, a polygon and a QR code. However, I find it rather difficult to get the proper image size (related to the label size which is 7.5 cm x 5.5 cm). I only get there by try and error (it is roughly (275, 204)). When saving, the resolution of the PNG is poor and so the print is.
How do I properly calculate the image size and how can I increase the resolution for saving the image as PNG (or JPG or JPEG).
Here is my code so far:
import qrcode
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
# Create qr code instance
qr = qrcode.QRCode(
version = 1,
error_correction = qrcode.constants.ERROR_CORRECT_H,
box_size = 4,
border = .2,
)
text = "test"
# Create new image
img = Image.new('RGB', (275, 204), color = (220,220,220))
# Add data
qr.add_data(text)
qr.make(fit=True)
# Create an image from the QR Code instance
qrc = qr.make_image(fill_color='black', back_color='white')
draw = ImageDraw.Draw(img)
font1 = ImageFont.truetype("arial.ttf", 25)
font2 = ImageFont.truetype("arial.ttf", 60)
draw.text((65, 150),text,(0,0,0),font=font1)
draw.text((115, 120),text,(0,0,0),font=font2)
draw.polygon(((5, 20), (50, 34), (50, 6)), fill=0)
img.paste(qrc, (80,25))
img.show()
#img.save("image.png", dpi = (300,300))

