Skip to content

Instantly share code, notes, and snippets.

@sshh12
Created July 15, 2019 23:42
Show Gist options
  • Save sshh12/cfee187e45a83af6c4955ce6f7f98881 to your computer and use it in GitHub Desktop.
Save sshh12/cfee187e45a83af6c4955ce6f7f98881 to your computer and use it in GitHub Desktop.
Convert an image into a json array.
from PIL import Image
im = Image.open('banner.png').convert('RGB')
width, height = im.size
col_start = -1
for c in range(width):
content = False
for r in range(height):
color = im.getpixel((c, r))
if color != (255, 255, 255):
content = True
break
if content:
col_start = c
break
for r in range(height):
row_data = []
for c in range(col_start, width):
color = im.getpixel((c, r))
if color == (255, 255, 255):
val = 0
else:
val = 4
row_data.append(val)
print(row_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment