1
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
3
# Copyright (C) 2012 David Planella <david.planella@ubuntu.com>
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License version 3, as published
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License version 3, as published
6
6
# by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
11
# PURPOSE. See the GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License along
13
# You should have received a copy of the GNU General Public License along
14
14
# with this program. If not, see <http://www.gnu.org/licenses/>.
21
print "You need to install the python-qrencode package"
42
47
self.cr_surface = None
44
def encode(self, text, output_type=QRCodeOutput.PIL_IMAGE):
49
def encode(self, text, current_color_fg=None, previous_color_fg=None,
50
current_color_bg=None, previous_color_bg=None, border=True,
51
output_type=QRCodeOutput.PIL_IMAGE):
45
52
'''Encodes the given text and returns a QR code in the given format'''
47
54
if output_type == QRCodeOutput.PIL_IMAGE:
48
qr_code = self._encode_to_pil(text)
55
qr_code = self._encode_to_pil(text, current_color_fg,
56
previous_color_fg, current_color_bg, previous_color_bg,
49
58
elif output_type == QRCodeOutput.CAIRO_SURFACE:
50
qr_code = self._encode_to_cairo(text)
59
qr_code = self._encode_to_cairo(text, current_color_fg,
60
previous_color_fg, current_color_bg, previous_color_bg,
54
def _encode_to_cairo(self, text):
65
def _encode_to_cairo(self, text, current_color_fg=None,
66
previous_color_fg=None, current_color_bg=None,
67
previous_color_bg=None, border=True):
55
68
'''Encodes the given text and returns a Cairo surface'''
56
self._encode_to_pil(text)
69
self._encode_to_pil(text, current_color_fg, previous_color_fg,
70
current_color_bg, previous_color_bg, border)
58
72
# Convert the Python PIL image to a Cairo surface.
59
73
# Notice the conversion to BGRA that Cairo expects
71
85
return self.cr_surface
73
def _encode_to_pil(self, text):
74
'''Encodes the given text and returns a Python PIL Image'''
87
def _encode_to_pil(self, text, current_color_fg=None,
88
previous_color_fg=None, current_color_bg=None,
89
previous_color_bg=None, border=True):
90
'''Encodes the given text and returns a Python PIL Image,
91
changing colors if necessary'''
93
# Returns an 8-bit pixel, black and white image (mode L)
75
94
version, self.qrcode_size, self.image = qrencode.encode(text)
96
if (current_color_fg != previous_color_fg) or \
97
(current_color_bg != previous_color_bg):
98
self.image = ImageOps.colorize(self.image,
101
current_color_fg[2]),
102
(current_color_bg[0],
104
current_color_bg[2]))
106
# Returns a 4x8-bit pixel, true colour with transparency mask image
76
108
self.image = self.image.convert('RGBA')
112
self._add_border(current_color_bg)
107
141
self.pixel_size = pixel_size
109
def _add_border(self):
143
def _add_border(self, current_color_bg=None):
110
144
'''Adds a border to the QR code'''
146
fill = (current_color_bg[0], current_color_bg[1],
147
current_color_bg[2], 255)
112
151
border_size = (self.output_size - self.image.size[0]) / 2
113
152
self.image = ImageOps.expand(self.image, border=border_size,