Output the QR Code using ASCII characters. :param tty: use fixed TTY color codes (forces invert=True) :param invert: invert the ASCII characters (solid <-> transparent)
(self, out=None, tty=False, invert=False)
| 284 | out.flush() |
| 285 | |
| 286 | def print_ascii(self, out=None, tty=False, invert=False): |
| 287 | """ |
| 288 | Output the QR Code using ASCII characters. |
| 289 | |
| 290 | :param tty: use fixed TTY color codes (forces invert=True) |
| 291 | :param invert: invert the ASCII characters (solid <-> transparent) |
| 292 | """ |
| 293 | if out is None: |
| 294 | out = sys.stdout |
| 295 | |
| 296 | if tty and not out.isatty(): |
| 297 | raise OSError("Not a tty") |
| 298 | |
| 299 | if self.data_cache is None: |
| 300 | self.make() |
| 301 | |
| 302 | modcount = self.modules_count |
| 303 | codes = [bytes((code,)).decode("cp437") for code in (255, 223, 220, 219)] |
| 304 | if tty: |
| 305 | invert = True |
| 306 | if invert: |
| 307 | codes.reverse() |
| 308 | |
| 309 | def get_module(x, y) -> int: |
| 310 | if invert and self.border and max(x, y) >= modcount + self.border: |
| 311 | return 1 |
| 312 | if min(x, y) < 0 or max(x, y) >= modcount: |
| 313 | return 0 |
| 314 | return cast(int, self.modules[x][y]) |
| 315 | |
| 316 | for r in range(-self.border, modcount + self.border, 2): |
| 317 | if tty: |
| 318 | if not invert or r < modcount + self.border - 1: |
| 319 | out.write("\x1b[48;5;232m") # Background black |
| 320 | out.write("\x1b[38;5;255m") # Foreground white |
| 321 | for c in range(-self.border, modcount + self.border): |
| 322 | pos = get_module(r, c) + (get_module(r + 1, c) << 1) |
| 323 | out.write(codes[pos]) |
| 324 | if tty: |
| 325 | out.write("\x1b[0m") |
| 326 | out.write("\n") |
| 327 | out.flush() |
| 328 | |
| 329 | @overload |
| 330 | def make_image( |