Skip to content

Instantly share code, notes, and snippets.

@suapapa
Created June 15, 2012 07:03
Show Gist options
  • Save suapapa/2935113 to your computer and use it in GitHub Desktop.
Save suapapa/2935113 to your computer and use it in GitHub Desktop.
Convert image file to C byte array
#!/usr/bin/python
import sys
import Image
fileName = sys.argv[1]
img = Image.open(fileName)
img = img.convert('L')
w, h = img.size
data = img.tostring()
name = fileName[:fileName.rfind('.')]
name = name.replace(' ', '_')
line_buffer = []
print 'char %s_%dx%d[] = \\'%(name, w, h)
for p in data:
line_buffer.append("\\x%02x"%ord(p))
if len(line_buffer) >= 18:
print ' "%s"\\'%(''.join(line_buffer))
line_buffer = []
print ";"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment