Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- from PIL import Image
- import math
- import sys
- """
- le one and only:
- textwall image converter™
- by KKosty4ka
- version 1.0.0
- usage:
- ./textwall_image_converter.py image.png image.txt
- the image msut have even height cuz im lazy
- now go to textwall and spam goatse all over spawn
- """
- PALLETE = {
- 0x80: (0, 0, 0),
- 0x9e: (81, 82, 82),
- 0x81: (137, 141, 144),
- 0x82: (212, 215, 217),
- 0x97: (109, 0, 26),
- 0x8f: (190, 0, 57),
- 0x84: (255, 69, 0),
- 0x85: (255, 168, 0),
- 0x87: (255, 214, 53),
- 0x98: (255, 248, 184),
- 0x90: (0, 163, 104),
- 0x89: (0, 204, 120),
- 0x88: (126, 237, 86),
- 0x91: (0, 117, 111),
- 0x92: (0, 158, 170),
- 0x99: (0, 204, 192),
- 0x8c: (36, 80, 164),
- 0x8b: (54, 144, 234),
- 0x8a: (81, 233, 244),
- 0x93: (73, 58, 193),
- 0x94: (106, 92, 255),
- 0x9a: (148, 179, 255),
- 0x8e: (129, 30, 159),
- 0x8d: (180, 74, 192),
- 0x9b: (228, 171, 255),
- 0x9c: (222, 16, 127),
- 0x95: (255, 56, 129),
- 0x83: (255, 153, 170),
- 0x96: (109, 72, 47),
- 0x86: (156, 105, 38),
- 0x9d: (255, 180, 112)
- }
- img = Image.open(sys.argv[1])
- width, height = img.size
- pix = img.load()
- output = ("█" * width + "\n") * (height // 2) + "\x1b"
- output = bytearray(output, encoding="utf8")
- def color_distance(a: tuple, b: tuple) -> float:
- return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2)
- def nearest_color(color: tuple) -> int:
- nearest = 0
- nearest_dist = 9999999999
- for k, v in PALLETE.items():
- dist = color_distance(color, v)
- if dist < nearest_dist:
- nearest = k
- nearest_dist = dist
- return nearest
- for y in range(0, height, 2):
- for x in range(width):
- col1 = pix[x, y]
- col2 = pix[x, y + 1]
- avg = (
- (col1[0] + col2[0]) // 2,
- (col1[1] + col2[1]) // 2,
- (col1[2] + col2[2]) // 2
- )
- output.append(0xc3)
- output.append(nearest_color(avg))
- output.append(0xc3)
- output.append(0x80)
- with open(sys.argv[2], "wb") as f:
- f.write(output)
Add Comment
Please, Sign In to add comment