Tôi muốn viết một số tác phẩm BMP đơn giản, vì vậy tôi nghiên cứu các định dạng và viết rất đơn giản bmp.py module:
# get bmp.py at http://www.ptmcg.com/geo/python/bmp.py.txt
from bmp import BitMap, Color
from itertools import product
from random import randint, choice
# make a list of 256 colors (all you can fit into an 8-bit BMP)
colors = [Color(randint(0,255), randint(0,255), randint(0,255))
for i in xrange(256)]
bmp = BitMap(100,100)
for x,y in product(xrange(100),xrange(100)):
bmp.setPenColor(choice(colors))
bmp.plotPoint(x,y)
bmp.saveFile("100x100.bmp", compress=False)
mẫu 100x100.bmp:

Đối với kích thước pixel lớn hơn một chút, hãy sử dụng:
PIXEL_SIZE=5
bmp = BitMap(PIXEL_SIZE*100,PIXEL_SIZE*100)
for x,y in product(xrange(100),xrange(100)):
bmp.setPenColor(choice(colors))
bmp.drawSquare(x*PIXEL_SIZE,y*PIXEL_SIZE,PIXEL_SIZE,fill=True)
filename = "%d00x%d00.bmp" % (PIXEL_SIZE,PIXEL_SIZE)
bmp.saveFile(filename)

Bạn không thể muốn sử dụng bmp.py, nhưng điều này cho thấy bạn ý tưởng chung của những gì bạn sẽ cần phải làm.
Cụ thể bạn đã thử gì? – tacaswell
(Tức là, hãy đảm bảo đăng mã/nỗ lực hiện tại và mô tả cách chúng không hoạt động như mong đợi.) –