• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Python实现生成bmp图像的方法
    POST TIME:2021-10-18 13:31

    之前使用过c、java、go语言实现过生成纯色BMP图片的功能。

    现在由python语言完成该功能。

    from array import array
    
    class bmp:
        """ bmp data structure """
    
        def __init__(self, w=1080, h=1920, color = 0xffffff):
            self.w = w
            self.h = h
            self.gen_bmp_header()
            self.paint_bgcolor(color)
    
        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 paint_point(self, x, y, color=0x000000):
            self.rgbData[y][x] = color
    
        def save_image(self, name="save.bmp"):
            f = open(name, 'wb')
    
            #write bmp header
            f.write(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('B', l).tobytes())
    
                for i in range(zeroBytes):
                    f.write(bytes([0x00]))
    
            #close file
            f.close()
    
    if __name__ == '__main__':
    
    
        image = bmp(35, 35)
    
        for i in range(35):
            image.paint_point(i, i, 0xff0000)
    
        image.save_image("save1.bmp")
        import os
        os.system("save1.bmp")
    
    

    到此这篇关于Python实现生成bmp图像的方法的文章就介绍到这了,更多相关Python生成bmp图像内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • python bmp转换为jpg 并删除原图的方法
    上一篇:Python实现随机生成迷宫并自动寻路
    下一篇:python实现A*寻路算法
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信