Welcome to easyticket_qrgen’s documentation!

Provides methods to create tickets from a template and create QR codes for them.

Here’s a short example on how to generate a ticket token and the qr code for it.

Example

>>> token = create_ticket_token()
>>> qr = create_qr(token)

See the method documentation for more parameters.

After a qr code is generated you can place it on a template image. In the following example the image template is assumed to be in the file “~/Pictures/ticket_template.png” and the QR code is placed on position (95, 109).

Example

>>> # load image
>>> template_img = Image.open('~/Pictures/ticket_template.png')
>>> # create a template object
>>> ticket_template = TicketTemplate(template_img)
>>> # need to call only once: always place qr code on that position
>>> ticket_template.add_qr_code((95, 109))
>>> # render qr code to PIL image
>>> content = render_pil(qr)
>>> # place this qr code on the ticket
>>> ticket = ticket_template.render({'qr_code': content})
>>> # the same template can be used to render more codes on the ticket
>>> ticket.show()
class easyticket_qrgen.qr_ticketgen.ImagePlaceable(corner, scale_to=None)[source]

An implementation of Placeable that pastes another image into the template.

In this case the content argument of place must be a PIL.Image.Image.

corner

The point on which the other image is placed in the template in the form (x, y).

Type:two element tuple of ints
scale_to

If given the image pasted onto the template gets scaled to this dimension (width, height), otherwise the image is not scaled.

Type:two element tuple of ints or None
place(image, content)[source]

Place something on the template image.

The type of content depends on the implementation, this can be another image or a text.

Parameters:
  • image (PIL.Image.Image) – The ticket template to be filled with content.
  • content – The type depends on the implementation, could be a string or another image.
Returns:

Changes the image in-place.

Return type:

None

class easyticket_qrgen.qr_ticketgen.Placeable[source]

An abstract base class for all objects that add something to a template image.

Each implementation places something on the template image, another image or text with its place method.

place(image, content)[source]

Place something on the template image.

The type of content depends on the implementation, this can be another image or a text.

Parameters:
  • image (PIL.Image.Image) – The ticket template to be filled with content.
  • content – The type depends on the implementation, could be a string or another image.
Returns:

Changes the image in-place.

Return type:

None

class easyticket_qrgen.qr_ticketgen.TicketTemplate(img, placables=None)[source]

Class that represents a ticket template.

It consists of a template image that is filled with content by Placeable objects.

Attributes:
img (PIL.Image.Image): The ticket template as a PIL image. placables (dict string to Placeable): Maps names for placables to a concrete Placable implementation.
add_qr_code(corner, scale_to=None)[source]

Add an entry to the placables dictionary with name ‘qr_code’ that places a generated qr code to the template.

It simply creates an ImagePlaceable with the specified arguments.

Parameters:corner (two element tuple of ints) – The point on which the QR code is placed in the template in the form (x, y).
scale_to (two element tuple of ints or None): If given the QR code pasted onto the template gets scaled to
this dimension (width, height), otherwise the image is not scaled. The QR code should not be scaled but should be created with the right size before.
render(contents)[source]

Applies all placables on the given template image.

contents is a dictionary mapping the names from placables to the actual content. For example the placable added by add_qr_code has the name ‘qr_code’ and thus in content there should be an entry ‘qr_code’ mapping to a PIL image of the QR code.

Only the placables with a key in contents will be activated.

Parameters:contents (dict) – Mapping the names from placables to the actual content.
Returns:The template when all objects in placables haven been applied (creates a copy)s.
Return type:PIL.Image.Image
easyticket_qrgen.qr_ticketgen.compute_qr_size(qr, scale, quiet_zone)[source]

Compute the size of the QR code when rendered to a PIL image in pixels.

Because QR codes are squares this is the width and height of the image.

Parameters:
  • qr (pyqrcode.QRCode) – The QR code to compute the size of.
  • scale (int) – The size in pixels of on module of the QR code (one block in the code).
  • quiet_zone (int) – The size of blocks left blank in the generated image, that is quiet_zone=4 leaves 4*scale free space on each side of the image.
Returns:

The size of the generated image in pixels.

Return type:

int

easyticket_qrgen.qr_ticketgen.create_qr(token, error='M', **kwargs)[source]

Generates a QR code for a given token.

The token can be generated with create_ticket_token, a QR code instance from pyqrcode is returned.

Parameters:
  • token (str) – The token to encode in the QR code.
  • error (str) – The error level of the QR code (‘L’, ‘M’, ‘Q’, ‘H’), ‘L’ allowst the least number of errors, ‘H’ the most.
  • **kwargs (dict) – All additional arguments passed to pyqrcode.create.
Returns:

The QR code of the token.

Return type:

pyqrcode.QRCode

easyticket_qrgen.qr_ticketgen.create_ticket_token(length=64, mode='alphanumeric')[source]

Creates a token for a ticket.

The output is a cryptographically strong random sequence.

Parameters:
  • length (int) – The length of the token (number of characters).
  • mode (str) – Either ‘int’ for a sequence of integer values or ‘alphanumeric’ for valid QR code chars.
Returns:

The string representation of the generated token with the given length.

Return type:

str

easyticket_qrgen.qr_ticketgen.render_pil(qr, module_color='black', bg='white', scale=4, quiet_zone=4)[source]

Render a QR code as a PIL image.

The qr code can obtained with create_qr, this QR code is then rendered to a new PIL image.

Parameters:
  • qr (pyqrcode.QRCode) – The QR code to render.
  • module_color (PIL color) – The color to use for the modules (‘blocks’) in the QR code.
  • bg (PIL color) – The background color of the QR code.
  • scale (int) – The size in pixels of on module of the QR code (one block in the code).
  • quiet_zone (int) – The size of blocks left blank in the generated image, that is quiet_zone=4 leaves 4*scale free space on each side of the image.
Returns:

The QR code rendered to a PIL image.

Return type:

PIL.Image.Image

easyticket_qrgen.qr_ticketgen.secret_sequence(num, sequence)[source]

Creates a random sequence (cryptographically strong random sequence).

Randomly chooses num elements from sequence and returns the random sequence as a string.

Parameters:
  • num (int) – The length of the output sequence.
  • sequence (str) – The elements to choose from as a string, each char one possible option.
Returns:

The random sequence as a string (num random elements from sequence combined).

Return type:

str

Indices and tables