python没有库,没有库的python代码

使用BMP file format可以简单地创建自己的BMP文件:import math

import struct

width = 200

height = 200

bits_per_pixel = 24

# bitmap file header

data = 'BM' # Windows 3.1x, 95, NT, ... etc.

data += struct.pack('i', 26 + bits_per_pixel / 8 * width * height) # The size of the BMP file in bytes = header + (color bytes * w * h)

data += struct.pack('h', 0) # Reserved

data += struct.pack('h', 0) # Reserved

data += struct.pack('i', 26) # The offset, i.e. starting address, of the byte where the bitmap image data (pixel array) can be found.

# bitmap header (BITMAPCOREHEADER)

data += struct.pack('i', 12) # The size of this header (12 bytes)

data += struct.pack('h', width) # width

data += struct.pack('h', height) # height

data += struct.pack('h', 1) # The number of color planes, must be 1

data += struct.pack('h', bits_per_pixel) # The number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.

def rgb_to_bmp_data(r, g, b):

return chr(b) + chr(g) + chr(r)

row_bytes = width * (bits_per_pixel / 8)

row_padding = int(math.ceil(row_bytes / 4.0)) * 4 - row_bytes

for y in xrange(height - 1, -1, -1):

for x in xrange(width):

if (y / 25) % 2:

if (x / 25) % 2:

data += rgb_to_bmp_data(255, 255, 255)

else:

data += rgb_to_bmp_data(0, 0, 0)

else:

if (x / 25) % 2:

data += rgb_to_bmp_data(0, 0, 0)

else:

data += rgb_to_bmp_data(255, 255, 255)

# add padding

data += '\x00' * row_padding

with open('out.bmp', 'wb') as f:

f.write(data)

图像结果接近您需要的结果:

APtcL.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值