from PIL import Image, ImageDraw, ImageFont
def add_text_to_image(image_path, *texts_with_variables):
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('segoeui.ttf', 11) # The size is constant, it can be a fixed value
for variable, positions in texts_with_variables:
for position in positions:
draw.text(position, variable, (20, 20, 20), font=font) # Adjust these numbers to tweak the color
if image.mode in ("RGBA", "P"): # If the image has alpha channel, convert it to RGB
image = image.convert("RGB")
image.save('Reciept1.jpg')
variable1 = input('Enter the reciept item: ')
variable2 = input('Enter the reciept date: ')
variable3 = input('Enter the reciept price: ')
variable4 = input('Enter the reciept currency: ')
add_text_to_image('Template2.jpg',
(variable1, (167, 314), 11), # item
(variable2, (324, 187), 18), # date
(variable3, (531, 320), 11), # price
(variable3, (529, 432), 11), # price
(variable3, (480, 523), 34), # price
(variable3, (531, 619), 11), # price
('0.00', (535, 453), 11), # tax
(variable4, (525, 320), 11), # currency
(variable4, (523, 432), 11), # currency
(variable4, (463, 523), 34), # currency
(variable4, (525, 619), 11), # currency
(variable4, (529, 453), 11)) # currency
Traceback (most recent call last):
File "C:\Users\Josh\Desktop\Reciept Maker\Recieptv2.py", line 22, in <module>
add_text_to_image('Template2.jpg',
File "C:\Users\Josh\Desktop\Reciept Maker\Recieptv2.py", line 8, in add_text_to_image
for variable, positions in texts_with_variables:
ValueError: too many values to unpack (expected 2)
Tips