A pretty basic display function. Would work. Supports rescaling image according to the size of the terminal by skipping some pixels. Print X with red background if anything goes wrong
(self, rescale = True)
| 480 | |
| 481 | |
| 482 | def display(self, rescale = True): |
| 483 | """ |
| 484 | A pretty basic display function. Would work. Supports rescaling |
| 485 | image according to the size of the terminal by skipping some |
| 486 | pixels. Print X with red background if anything goes wrong |
| 487 | """ |
| 488 | if rescale: |
| 489 | try: |
| 490 | import os |
| 491 | w, h = os.get_terminal_size() |
| 492 | w //= 2 |
| 493 | scalar = max(max(1, self.width // w), max(1, self.height // h)) |
| 494 | except ModuleNotFoundError: |
| 495 | print("Unable to load os module. Unable to rescale the image accordingly") |
| 496 | scalar = 1 |
| 497 | else: |
| 498 | scalar = 1 |
| 499 | |
| 500 | for y in range(0, self.height, scalar): |
| 501 | for x in range(0, self.width, scalar): |
| 502 | try: |
| 503 | if self.bit_depth != 16: |
| 504 | color = ";".join(map(str, self.pixels[y][x][:-1])) |
| 505 | else: |
| 506 | color = ";".join(map(lambda v: str(v//256), self.pixels[y][x][:-1])) |
| 507 | print(f"\033[38;2;{color}m██", end="") |
| 508 | except: |
| 509 | print(f"\033[41mXX\033[0m", end="") |
| 510 | print("\033[0m") |
| 511 | |
| 512 | |
| 513 | def write_as_bmp(img, path="image.bmp"): |
no outgoing calls
no test coverage detected