五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

Python實(shí)現(xiàn)生成bmp圖像的方法

發(fā)布日期:2022-03-12 09:56 | 文章來源:CSDN

之前使用過c、java、go語言實(shí)現(xiàn)過生成純色BMP圖片的功能。

現(xiàn)在由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")

到此這篇關(guān)于Python實(shí)現(xiàn)生成bmp圖像的方法的文章就介紹到這了,更多相關(guān)Python生成bmp圖像內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

美國(guó)服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部