-
Notifications
You must be signed in to change notification settings - Fork 0
/
pseudoqr.py
executable file
·257 lines (209 loc) · 10.3 KB
/
pseudoqr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/python3
######################################################
# pseudoqr
# Script to generate a qr code which can't be scanned
#
# Author: Kippi
# Version: 1.0
######################################################
# --- BEGIN IMPORTS (DO NOT EDIT) ---
import argparse
import math
import numpy
import os
import sys
from enum import IntEnum
from PIL import Image
# --- END IMPORTS
# --- BEGIN CONFIG ---
# The size of the modules (points) in px
module_size = 4
# The width of the margin surrounding the qr code in modules
margin = 4
# The qr code symbol version to use. See http://www.qrcode.com/en/about/version.html for details. This option is mutually exclusive with modules
symbol_version = None
# The number of modules to use in each direction (horizontally and vertically). This option is mutually exclusive with symbol_version
modules = None
# RGB value to use as foreground
foreground_pixel = [0, 0, 0]
# RGB value to use as background
background_pixel = [255, 255, 255]
# Defines if a fourth edge should be drawn
draw_fourth_edge = False
# Defines the image format. If undefined (None), derive image format from filename
image_format = None
# The output file name
filename = None
# --- END CONFIG ---
# DO NOT EDIT BELOW THIS LINE IF YOU DO NOT KNOW WHAT YOU ARE DOING
EDGE_MODULES = 7
ALIGNMENT_MODULES = 5
ALIGNMENT_PADDING = 4
ALIGNMENT_MAX_SPACE = 23
STDOUT_FILENAME = "-"
class Direction(IntEnum):
UP = 1
DOWN = 2
LEFT = 4
RIGHT = 8
def parse_color(color, default):
if color is None:
return default
else:
return [int(color[i:i+2], 16) for i in range(0, len(color), 2)]
def create_image(output, pixels):
array = numpy.array(pixels, dtype=numpy.uint8)
new_image = Image.fromarray(array)
if filename == STDOUT_FILENAME:
output = sys.stdout
new_image.save(output, format=image_format)
def draw_module(x, y, pixel, pixels):
for i in range(0, module_size):
for j in range(0, module_size):
pixels[x + i][y + j] = pixel.copy()
def draw_dot(x, y, size, pixels, padding_direction=0):
if size % 2 == 0 or size < 5:
raise ValueError("\"size\" must be odd and greater or equal 5")
for i in range(0, size):
for j in range(0, size):
if (j % (size - 3) == 1 and not i % (size - 1) == 0) or (i % (size - 3) == 1 and not j % (size - 1) == 0):
pixel_to_use = background_pixel
else:
pixel_to_use = foreground_pixel
draw_module(x + i * module_size, y + j * module_size, pixel_to_use, pixels)
up = padding_direction & Direction.UP == Direction.UP
down = padding_direction & Direction.DOWN == Direction.DOWN
left = padding_direction & Direction.LEFT == Direction.LEFT
right = padding_direction & Direction.RIGHT == Direction.RIGHT
start_add = 0 if not up else -module_size
end_add = 0 if not down else module_size
if up:
for i in range(0, size * module_size, module_size):
draw_module(x - module_size, y + i, background_pixel, pixels)
if down:
for i in range(0, size * module_size, module_size):
draw_module(x + size * module_size, y + i, background_pixel, pixels)
if left:
for i in range(0 + start_add, size * module_size + end_add, module_size):
draw_module(x + i, y - module_size, background_pixel, pixels)
if right:
for i in range(0 + start_add, size * module_size + end_add, module_size):
draw_module(x + i, y + size * module_size, background_pixel, pixels)
def main():
global module_size, margin, symbol_version, modules, foreground_pixel, background_pixel, draw_fourth_edge, image_format, filename
parser = argparse.ArgumentParser(description='Create a image which looks like a qr code but is unscanable')
parser.add_argument('-s',
'--module-size',
type=int,
action='store',
dest='module_size',
default=module_size,
required=False,
help='Specify module size in dots/pixels (default='+str(module_size)+').')
parser.add_argument('-m',
'--margin',
type=float,
action='store',
dest='margin',
default=margin,
required=False,
help='Specify the width of the margin in modules (default='+str(margin)+').')
parser.add_argument('-o',
'--output-file',
type=str,
action='store',
dest='filename',
required=filename is None,
default=filename,
help='Specify where the image should be saved. If \''+STDOUT_FILENAME+'\' is specified, the image will be written to standard output.')
command_group = parser.add_mutually_exclusive_group(required=True)
command_group.add_argument('-v',
'--symbol-version',
type=int,
action='store',
dest='symbol_version',
default=symbol_version,
help='The QR Code symbol version to use. See https://www.qrcode.com/en/about/version.html for details. This option is mutually exclusive with "modules"')
command_group.add_argument('-a',
'--modules',
type=int,
action='store',
dest='modules',
default=modules,
help='The number of modules to use in each direction (horizontally and vertically). This option is mutually exclusive with "symbol version"')
parser.add_argument('-d',
'--draw-fourth-edge',
action='store_true',
dest='draw_fourth_edge',
required=False,
default=draw_fourth_edge,
help='Generate a QR Code with 4 position elements ("edges").')
parser.add_argument('-f',
'--format',
type=str.upper,
action='store',
dest='format',
required=False,
default=image_format,
choices=["BMP", "DDS", "DIB", "EPS", "GIF", "ICNS", "ICO", "IM", "JPEG", "JPEG2000", "PCX", "PNG", "PPM", "SGI", "SPIDER", "TGA", "TIFF", "WEBP", "PDF"],
help='Specify the format of the resulting image. If omitted, the format is determined from the filename extension.')
parser.add_argument('--foreground',
type=str,
action='store',
dest='foreground',
required=False,
default=None,
help='Specifies the foreground color in hexadecimal RGB or RGBA notation (default='+("{:02X}" * len(foreground_pixel)).format(*foreground_pixel)+').')
parser.add_argument('--background',
type=str,
action='store',
dest='background',
required=False,
default=None,
help='Specifies the background color in hexadecimal RGB or RGBA notation (default='+("{:02X}" * len(background_pixel)).format(*background_pixel)+').')
args = parser.parse_args()
module_size = args.module_size
margin = args.margin
filename = args.filename
symbol_version = args.symbol_version
modules = args.modules if symbol_version is None else symbol_version * 4 + 17
draw_fourth_edge = args.draw_fourth_edge
image_format = args.format
foreground_pixel = parse_color(args.foreground, foreground_pixel)
background_pixel = parse_color(args.background, background_pixel)
margin_size = round(module_size * margin)
size = margin_size * 2 + module_size * modules
pixels = []
row = []
for i in range(0, size):
row.append(background_pixel.copy())
for i in range(0, size):
pixels.append(row.copy())
for i in range(0, modules * module_size, module_size):
for j in range(0, modules * module_size, module_size):
if int.from_bytes(os.urandom(1), "big") > 127:
draw_module(i + margin_size, j + margin_size, foreground_pixel, pixels)
edge_position = (modules - EDGE_MODULES) * module_size + margin_size
draw_dot(margin_size, margin_size, EDGE_MODULES, pixels, Direction.DOWN | Direction.RIGHT)
draw_dot(margin_size, edge_position, EDGE_MODULES, pixels, Direction.DOWN | Direction.LEFT)
draw_dot(edge_position, margin_size, EDGE_MODULES, pixels, Direction.UP | Direction.RIGHT)
if draw_fourth_edge:
draw_dot(edge_position, edge_position, EDGE_MODULES, pixels, Direction.UP | Direction.LEFT)
if modules >= 25:
base_alignment_position = (modules - ALIGNMENT_PADDING - ALIGNMENT_MODULES) * module_size + margin_size
alignment_padding_with_margin = margin_size + ALIGNMENT_PADDING * module_size
alignment_count = math.ceil((modules - 2 * ALIGNMENT_PADDING + ALIGNMENT_MAX_SPACE)/(ALIGNMENT_MAX_SPACE + ALIGNMENT_MODULES))
space = round((modules - 2 * ALIGNMENT_PADDING - alignment_count * ALIGNMENT_MODULES)/(alignment_count-1))
if space % 2 == 0:
space += 1
for i in range(0, alignment_count - 1):
for j in range(0, alignment_count - 1):
if not draw_fourth_edge or i + j > 0:
draw_dot(base_alignment_position - ((space + ALIGNMENT_MODULES) * i) * module_size, base_alignment_position - ((space + ALIGNMENT_MODULES) * j) * module_size, ALIGNMENT_MODULES, pixels)
for i in range(1, alignment_count - 1):
draw_dot(alignment_padding_with_margin, base_alignment_position - ((space + ALIGNMENT_MODULES) * i) * module_size, ALIGNMENT_MODULES, pixels)
for i in range(1, alignment_count - 1):
draw_dot(base_alignment_position - ((space + ALIGNMENT_MODULES) * i) * module_size, alignment_padding_with_margin, ALIGNMENT_MODULES, pixels)
create_image(filename, pixels)
if __name__ == '__main__':
main()