Last active
August 29, 2015 14:12
-
-
Save toyowata/bf576ee091e75071bc66 to your computer and use it in GitHub Desktop.
Convert image file to grayscale array data - 画像ファイルをグレースケール配列に変換する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#encoding=utf-8 | |
# Convert image to Pwmout grayscale data for dot-matrix LED | |
# Input image format can be JPEG, PNG etc | |
from PIL import Image | |
import sys | |
if __name__ == "__main__": | |
argvs = sys.argv | |
argc = len(argvs) | |
if (argc != 2): | |
print 'Usage: # python %s filename' % argvs[0] | |
quit() | |
im = Image.open(argvs[1]) | |
pix = im.load() | |
print "const float img[", im.size[0], "][", im.size[1], "] = {" | |
for y in range(0, im.size[1]): | |
for x in range(0, im.size[0]): | |
# conver RGB to grayscale | |
gray = (0.299 * pix[x,y][0] + 0.587 * pix[x,y][1] + 0.114 * pix[x,y][2]) | |
print ((255-gray)/256), "," | |
print "};" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment