Python:从PIL原始像素数据生成C数组

Python:从PIL原始像素数据生成C数组,python,python-imaging-library,Python,Python Imaging Library,我想从TrueType字体生成固定宽度的C头/包含。 到固定宽度位图的转换已经工作 import ImageFont, ImageDraw, Image fontSize = 32 fontWidth = 20 numFonts = 1 numChars = 127-32 # Because the first 32 characters are not visible. image = Image.new( 'RGB', (fontWidth*numChars,fontSize*numFo

我想从TrueType字体生成固定宽度的C头/包含。 到固定宽度位图的转换已经工作

import ImageFont, ImageDraw, Image

fontSize = 32
fontWidth = 20
numFonts = 1
numChars = 127-32 # Because the first 32 characters are not visible.

image = Image.new( 'RGB', (fontWidth*numChars,fontSize*numFonts), "black")
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("whitrabt.ttf", fontSize)
font2 = ImageFont.truetype("saxmono.ttf", fontSize)
font3 = ImageFont.truetype("MODENINE.TTF", fontSize)

# ASCII Characters from 32 DEC to 127 are visible
for x in range(32,127):
        draw.text(((x-32)*20, 0),chr( x), font=font)
        draw.text(((x-32)*20, 32),chr( x), font=font2)
        draw.text(((x-32)*20, 64),chr( x), font=font3)

// Convert to Grayscale for Grayscale LCD
image = image.convert('L')
image.show()
这工作正常,但不知何故我无法管理它
以C阵列形式输出像素数据。

您可以使用加载方法访问像素数据

如果要将所有像素放在一个大的C数组中,可以执行以下操作:

print 'const char pixels[] = {'
w, h = image.size    
for y in range(h):
    print '\t',
    for x in range(w):
        print pixels[x,y], ',',
    print
print '};'
对于灰度图像,像素将包含整数,对于彩色图像,它将是R、G、B元组

print 'const char pixels[] = {'
w, h = image.size    
for y in range(h):
    print '\t',
    for x in range(w):
        print pixels[x,y], ',',
    print
print '};'