2011. 11. 4. 02:36
import struct, random
#Function to write a bmp file. It takes a dictionary (d) of
#header values and the pixel data (bytes) and writes them
#to a file. This function is called at the bottom of the code.
def bmp_write(d,byte):
mn1 = struct.pack('<B',d['mn1'])
mn2 = struct.pack('<B',d['mn2'])
filesize = struct.pack('<L',d['filesize'])
undef1 = struct.pack('<H',d['undef1'])
undef2 = struct.pack('<H',d['undef2'])
offset = struct.pack('<L',d['offset'])
headerlength = struct.pack('<L',d['headerlength'])
width = struct.pack('<L',d['width'])
height = struct.pack('<L',d['height'])
colorplanes = struct.pack('<H',d['colorplanes'])
colordepth = struct.pack('<H',d['colordepth'])
compression = struct.pack('<L',d['compression'])
imagesize = struct.pack('<L',d['imagesize'])
res_hor = struct.pack('<L',d['res_hor'])
res_vert = struct.pack('<L',d['res_vert'])
palette = struct.pack('<L',d['palette'])
importantcolors = struct.pack('<L',d['importantcolors'])
#create the outfile
outfile = open('bitmap_image.bmp','wb')
#write the header + the bytes
outfile.write(mn1+mn2+filesize+undef1+undef2+offset+headerlength+width+height+\
colorplanes+colordepth+compression+imagesize+res_hor+res_vert+\
palette+importantcolors+byte)
outfile.close()
###################################
def main():
#Here is a minimal dictionary with header values.
#Of importance is the offset, headerlength, width,
#height and colordepth.
#Edit the width and height to your liking.
#These header values are described in the bmp format spec.
#You can find it on the internet. This is for a Windows
#Version 3 DIB header.
width = 2048
height = 2048
d = {
'mn1':66,
'mn2':77,
'filesize':width*height+54,
'undef1':0,
'undef2':0,
'offset':54,
'headerlength':40,
'width':width,
'height':height,
'colorplanes':0,
'colordepth':24,
'compression':0,
'imagesize':width*height,
'res_hor':0,
'res_vert':0,
'palette':0,
'importantcolors':0
}
#Function to generate a random number between 0 and 255
def rand_color():
x = random.randint(0,255)
return x
#Build the byte array. This code takes the height
#and width values from the dictionary above and
#generates the pixels row by row. The row_mod and padding
#stuff is necessary to ensure that the byte count for each
#row is divisible by 4. This is part of the specification.
byte = bytes()
for row in range(d['height']-1,-1,-1):# (BMPs are L to R from the bottom L row)
for column in range(d['width']):
b = rand_color()
g = rand_color()
r = rand_color()
pixel = struct.pack('<BBB',b,g,r)
byte = byte + pixel
row_mod = (d['width']*d['colordepth']/8) % 4
if row_mod == 0:
padding = 0
else:
padding = (4 - row_mod)
padbytes = bytes()
for i in range(padding):
x = struct.pack('<B',0)
padbytes = padbytes + x
byte = byte + padbytes
#call the bmp_write function with the
#dictionary of header values and the
#bytes created above.
bmp_write(d,byte)
if __name__ == '__main__':
main()
참고자료
http://docs.python.org/library/struct.html
http://pseentertainmentcorp.com/smf/index.php?topic=2034.0
http://en.wikipedia.org/wiki/BMP_file_format
'python' 카테고리의 다른 글
| 긴 문자열을 숫자로 바꾸는 코드 (0) | 2012.02.19 |
|---|---|
| [python] (가명) 속성 및 연산자 메쏘드 (0) | 2011.11.12 |
| [python] 복소수 complex 사용법 (0) | 2011.11.04 |
| [python] 삼항연산자 만들기 (1) | 2011.11.02 |
| [python] 집합 Sets (0) | 2011.11.02 |