Skip to content

Instantly share code, notes, and snippets.

@rodorgas
Created April 2, 2017 08:19
Show Gist options
  • Save rodorgas/35b631fdb5fefc2cc9f789ef816468d8 to your computer and use it in GitHub Desktop.
Save rodorgas/35b631fdb5fefc2cc9f789ef816468d8 to your computer and use it in GitHub Desktop.
Script to generate an template to reddit.com/place
import sys
from PIL import Image
class Template:
COLOR_MAP = {
(255, 255, 255): 0,
(228, 228, 228): 1,
(136, 136, 136): 2,
(34, 34, 34): 3,
(255, 167, 209): 4,
(229, 0, 0): 5,
(229, 149, 0): 6,
(160, 106, 66): 7,
(229, 217, 0): 8,
(148, 224, 68): 9,
(2, 190, 1): 10,
(0, 211, 221): 11,
(0, 131, 199): 12,
(0, 0, 234): 13,
(207, 110, 228): 14,
(130, 0, 128): 15,
}
def __init__(self, image):
self.image = Image.open(image)
self.width, self.height = self.image.size
self.pixels = self.image.getdata()
self.parse_image()
def rgb_to_reddit(self, color):
return str(self.COLOR_MAP[color])
def parse_image(self):
blocks = []
lines = []
i = 0
for pixel in self.pixels:
color = self.rgb_to_reddit(pixel)
lines.append(color.rjust(2))
i += 1
if i % self.width == 0:
blocks.append(lines)
lines = []
self.blocks = blocks
def make_template(self):
output = ''
for line in self.blocks:
for block in line:
output += block + ' '
output += '\n'
return output
if __name__ == '__main__':
template = Template(sys.argv[1])
print(template.make_template())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment