Previously used c, java, go language has been implemented to generate solid color BMP images.
The function is now done by the python language.
from array import array class bmp: """ bmp data structure """ def __init__(self, w=1080, h=1920, color = 0xffffff): = w = h self.gen_bmp_header() self.paint_bgcolor(color) def calc_data_size (self): if((*3)%4 == 0): = * 3 * else: = ((( * 3) // 4 + 1) * 4) * = + 54 def conv2byte(self, l, num, len): tmp = num for i in range(len): (tmp & 0x000000ff) tmp >>= 8 def gen_bmp_header (self): self.calc_data_size(); self.bmp_header = [0x42, 0x4d] self.conv2byte(self.bmp_header, , 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, , 4) self.conv2byte(self.bmp_header, , 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, , 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): = [] for r in range(): = [] for c in range(): (color) () 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) [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): [y][x] = color def paint_point(self, x, y, color=0x000000): [y][x] = color def save_image(self, name=""): f = open(name, 'wb') #write bmp header (array('B', self.bmp_header).tobytes()) #write rgb data zeroBytes = // - * 3 for r in range(): l = [] for i in range(len([r])): p = [r][i] (p & 0x0000ff) p >>= 8 (p & 0x0000ff) p >>= 8 (p & 0x0000ff) (array('B', l).tobytes()) for i in range(zeroBytes): (bytes([0x00])) #close file () if __name__ == '__main__': image = bmp(35, 35) for i in range(35): image.paint_point(i, i, 0xff0000) image.save_image("") import os ("")
to this article on the Python implementation of the method of generating bmp images is introduced to this article, more relevant Python to generate bmp image content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!