~thomas-deruyter-3/qreator/qreator

« back to all changes in this revision

Viewing changes to qreator/QRCode.py

  • Committer: Stefan Schwarzburg
  • Date: 2012-11-22 08:43:25 UTC
  • mfrom: (89.12.25 qreator)
  • mto: This revision was merged to the branch mainline in revision 101.
  • Revision ID: stefan.schwarzburg@googlemail.com-20121122084325-xubvujmrf0tz7d6r
merged with latest trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
2
### BEGIN LICENSE
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.
7
 
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 
 
7
#
 
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.
12
 
13
 
# You should have received a copy of the GNU General Public License along 
 
12
#
 
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/>.
15
15
### END LICENSE
16
16
 
17
 
import qrencode
 
17
import sys
 
18
try:
 
19
    import qrencode
 
20
except ImportError:
 
21
    print "You need to install the python-qrencode package"
 
22
    sys.exit(1)
18
23
import Image
19
24
import ImageOps
20
25
import cairo
41
46
        self.image = None
42
47
        self.cr_surface = None
43
48
 
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'''
46
53
 
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,
 
57
                border)
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,
 
61
                border)
51
62
 
52
63
        return qr_code
53
64
 
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)
57
71
 
58
72
        # Convert the Python PIL image to a Cairo surface.
59
73
        # Notice the conversion to BGRA that Cairo expects
70
84
 
71
85
        return self.cr_surface
72
86
 
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'''
 
92
 
 
93
        # Returns an 8-bit pixel, black and white image (mode L)
75
94
        version, self.qrcode_size, self.image = qrencode.encode(text)
 
95
 
 
96
        if (current_color_fg != previous_color_fg) or \
 
97
           (current_color_bg != previous_color_bg):
 
98
            self.image = ImageOps.colorize(self.image,
 
99
                                           (current_color_fg[0],
 
100
                                           current_color_fg[1],
 
101
                                           current_color_fg[2]),
 
102
                                           (current_color_bg[0],
 
103
                                           current_color_bg[1],
 
104
                                           current_color_bg[2]))
 
105
 
 
106
        # Returns a 4x8-bit pixel, true colour with transparency mask image
 
107
        # (mode RGBA)
76
108
        self.image = self.image.convert('RGBA')
 
109
        
77
110
        self._resize()
78
 
        self._add_border()
 
111
        if border:
 
112
            self._add_border(current_color_bg)
79
113
 
80
114
        return self.image
81
115
 
106
140
 
107
141
        self.pixel_size = pixel_size
108
142
 
109
 
    def _add_border(self):
 
143
    def _add_border(self, current_color_bg=None):
110
144
        '''Adds a border to the QR code'''
 
145
        if current_color_bg:
 
146
            fill = (current_color_bg[0], current_color_bg[1],
 
147
                    current_color_bg[2], 255)
 
148
        else:
 
149
            fill = 'white'
111
150
        # Add a border
112
151
        border_size = (self.output_size - self.image.size[0]) / 2
113
152
        self.image = ImageOps.expand(self.image, border=border_size,
114
 
                                     fill='white')
 
153
                                     fill=fill)