我需要一段python代码,实现生成分辨率1920*1080,位深24的bmp图片
时间: 2024-02-11 14:05:12 浏览: 233
以下是实现该功能的Python代码:
```python
import struct
# 设置图片宽高和位深
width = 1920
height = 1080
depth = 24
# 计算文件大小和像素数组大小
pixel_array_size = width * height * (depth // 8)
file_size = 54 + pixel_array_size
# BMP文件头
bmp_header = struct.pack('<ccIHHI', b'B', b'M', file_size, 0, 0, 54)
# BMP信息头
bmp_info_header = struct.pack('<IiiHHIIiiII', 40, width, height, 1, depth, 0, pixel_array_size, 0, 0, 0, 0)
# 生成像素数组
pixel_data = bytearray(width * height * 3)
for y in range(height):
for x in range(width):
pixel_data[(y * width + x) * 3] = 255 # B
pixel_data[(y * width + x) * 3 + 1] = 0 # G
pixel_data[(y * width + x) * 3 + 2] = 0 # R
# 写入文件
with open('output.bmp', 'wb') as f:
f.write(bmp_header)
f.write(bmp_info_header)
f.write(pixel_data)
```
这段代码会生成一个分辨率为1920x1080,位深为24的红色图片,保存在当前目录下的output.bmp文件中。你可以根据需要修改像素数组的生成方式来生成不同的图片。
阅读全文
相关推荐


















