Skip to content

Instantly share code, notes, and snippets.

@SheatNoisette
Forked from ColtonPhillips/pzr.py
Last active March 28, 2019 14:59
Show Gist options
  • Save SheatNoisette/0547e5c36f47c9ff03b3d0d1a60f372d to your computer and use it in GitHub Desktop.
Save SheatNoisette/0547e5c36f47c9ff03b3d0d1a60f372d to your computer and use it in GitHub Desktop.
Small refactoring and added simple CLI
from PIL import Image
import sys
def addUnique(l,item):
if item not in l:
l.append(item)
return l
def convertImage(image):
#Open image
im = Image.open(image)
pix = im.load()
#first create the legend
# Creates a list containing 5 lists initialized to 0
out_l_l = [[0 for x in range(im.size[0])] for x in range(im.size[1])]
col_set = []
for j in range(im.size[0]):
for i in range(im.size[1]):
r, g, b, a = pix[i,j]
curPixel = '#%02x%02x%02x' % (r, g, b)
if a != 0:
addUnique(col_set, curPixel)
line = ""
for item in col_set:
line += item + " "
print(line)
for j in range (im.size[0]):
line = ""
for i in range (im.size[1]):
r,g,b,a = pix[i,j]
if (a == 0):
line += "."
else:
curPixel = '#%02x%02x%02x' % (r, g, b)
line += str(col_set.index(curPixel))
print(line)
#Entry module
if __name__ == "__main__":
if (len(sys.argv) == 2):
convertImage(sys.argv[1])
else:
print("Usage: <file>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment