3

I am creating QRcode for each ticket. The QRcode is in image format. enter image description here

I want to extend this image vertically on both the sides (i.e. Top and Bottom). And in the extended area I want to add some additional data like this:

Theater Name
_____________________
|                   |
|                   |
|   QR CODE         |
|                   |
|                   |
|                   |
|                   |
|                   |
|___________________|

Seat id: 24 C
Movie: The baby boss
Showtime: 2:30 pm
Date: 09/04/17

Now I am doing this way to add image:

image = Image.open('qrcode.jpg')
width, height = image.size
draw = ImageDraw.Draw(image)
text_cinema = "RPG Cinema"
text_seat = "Seat: 15C Balcony"
text_movie = "The baby boss"
text_showtime = "02:30 pm"
text_date = "09/04/17"
font = ImageFont.truetype('font.ttf', 24)
draw.text((40, 5), text_cinema, font=font)
draw.text((40,height - 40), text_seat, font=font)
draw.text((40,height + 40), text_movie, font=font)
draw.text((40,height + 40), text_showtime, font=font)
image.save(str(time()).replace('.', '_') + '.jpg')

And the image turns in this way:

enter image description here

As you can see, the text are added into the image and not extending the image itself. Also, other data like text_movie, text_showtime and text_date are not being inserted as the text does not extend the image.

How can I extend the image and insert texts into the extended image area?

Aamu
  • 3,431
  • 7
  • 39
  • 61
  • Here's a suggestion for expanding the image: https://stackoverflow.com/questions/1572691/in-python-python-image-library-1-1-6-how-can-i-expand-the-canvas-without-resiz (my earlier comment linked to the wrong question). – Craig Apr 08 '17 at 14:45

1 Answers1

5

First create a larger image on which to write the text, then paste the QR into that image taken as a background.

>>> from PIL import Image
>>> background = Image.new('RGBA', (176, 225), (255,255,255,255))
>>> from PIL import ImageDraw
>>> draw = ImageDraw.Draw(background)
>>> draw.text((5,5), "Top text", (0,0,0))
>>> draw.text((5,210),"Bottom", (0,0,0))
>>> qr = Image.open('qr.png')
>>> background.paste(qr, (0,20))
>>> background.save('back.png')

In this case the QR is 176x176. The resulting image is 176x225. I haven't taken the trouble to define a non-default font.

Here's the result.

result

Bill Bell
  • 21,021
  • 5
  • 43
  • 58