Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convert image file to grayscale array data - 画像ファイルをグレースケール配列に変換する
#!/usr/bin/env python
# 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]
im =[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
You can’t perform that action at this time.