Python创建bmp 文件的教程进链接:
点击进链接
代码实现:
`class bmp:
#bmp data structure
def __init__(self, w=1080, h=1920):
self.w = w
self.h = h
def calc_data_size (self):
if((self.w*3)%4 == 0):
self.dataSize = self.w * 3 * self.h
else:
self.dataSize = (((self.w * 3) // 4 + 1) * 4) * self.h
self.fileSize = self.dataSize + 54
def conv2byte(self, l, num, len):
tmp = num
for i in range(len):
l.append(tmp & 0x000000ff)
tmp >>= 8
def gen_bmp_header (self):
self.calc_data_size();
self.bmp_header = [0x42, 0x4d]
self.conv2byte(self.bmp_header, self.fileSize, 4) #file size
self.conv2byte(self.bmp_header, 0, 2)
self.conv2byte(self.bmp_header, 0, 2)
self.conv2byte(self.bmp_header, 54, 4) #rgb data offset
self.conv2byte(self.bmp_header, 40, 4) #info block size
self.conv2byte(self.bmp_header, self.w, 4)
self.conv2byte(self.bmp_header, self.h, 4)
self.conv2byte(self.bmp_header, 1, 2)
self.conv2byte(self.bmp_header, 24, 2) #888
self.conv2byte(self.bmp_header, 0, 4) #no compression
self.conv2byte(self.bmp_header, self.dataSize, 4) #rgb data size
self.conv2byte(self.bmp_header, 0, 4)
self.conv2byte(self.bmp_header, 0, 4)
self.conv2byte(self.bmp_header, 0, 4)
self.conv2byte(self.bmp_header, 0, 4)
def print_bmp_header (self):
length = len(self.bmp_header)
for i in range(length):
print("{:0>2x}".format(self.bmp_header[i]), end=' ')
if i%16 == 15:
print('')
print('')
def paint_bgcolor(self, color=0xffffff):
self.rgbData = []
for r in range(self.h):
self.rgbDataRow = []
for c in range(self.w):
self.rgbDataRow.append(color)
self.rgbData.append(self.rgbDataRow)
def paint_line(self, x1, y1, x2, y2, color):
k = (y2 - y1) / (x2 - x1)
for x in range(x1, x2+1):
y = int(k * (x - x1) + y1)
self.rgbData[y][x] = color
def paint_rect(self, x1, y1, w, h, color):
for x in range(x1, x1+w):
for y in range(y1, y1+h):
self.rgbData[y][x] = color
def save_image(self, name="save.bmp"):
f = open(name, 'wb')
#write bmp header
f.write(array.array('B', self.bmp_header).tobytes())
#write rgb data
zeroBytes = self.dataSize // self.h - self.w * 3
for r in range(self.h):
l = []
for i in range(len(self.rgbData[r])):
p = self.rgbData[r][i]
l.append(p & 0x0000ff)
p >>= 8
l.append(p & 0x0000ff)
p >>= 8
l.append(p & 0x0000ff)
f.write(array.array('B', l).tobytes())
for i in range(zeroBytes):
f.write(bytes(0x00))
#close file
f.close()
`
用python读取bpm图片教程:
点击进教程
代码实现:
`f = open("save1.bmp", 'rb')
bmp_header_b = f.read(0x36)
print(bmp_header_b)
print('')
bmp_header_s = struct.unpack('<2sI2H4I2H6I', bmp_header_b)
print(bmp_header_s)
print('')`
把元组转化为class对象
`class bmp:
"data structure"
def __init__(self, tp):
self.tag = tp[0]
self.fileSize = tp[1]
#reserved tp[2]
#reserved tp[3]
self.rgbOffset = tp[4]
self.infoSize = tp[5]
self.width = tp[6]
self.height = tp[7]
self.pane = tp[8]
self.color = tp[9]
self.compress = tp[10]
self.rgbSize = tp[11]
#reserved tp[12]
#reserved tp[13]
#reserved tp[14]
#reserved tp[15]
def print_bmp_header(self):
print("tag :{}".format(self.tag))
print("fileSize :{}".format(self.fileSize))
print("rgbOffset:{}".format(self.rgbOffset))
print("infoSize :{}".format(self.infoSize))
print("width :{}".format(self.width))
print("height :{}".format(self.height))
print("pane :{}".format(self.pane))
print("color :{}".format(self.color))
print("compress :{}".format(self.color))
print("rgbSize :{}".format(self.rgbSize))
if name == 'main':
# ...
image = bmp(bmp_header_s)
image.print_bmp_header()
print('')
读取RGB数据
# bytearray
bmp_rgb_data_b = f.read()
bytearray -> list
list_b = array.array('B', bmp_rgb_data_b).tolist()
reshape -> 3d list
rgb_data_3d_list = numpy.reshape(list_b, (image.height, image.width, 3)).tolist()`