202408830使用python3给BGR3的裸图加上BMP图的文件头

202408830使用python3给BGR3的裸图加上文件头
2024/8/30 20:20


缘起,获取SONY索尼的8530的机芯的4K的BGR3的裸图。可以使用7-yuv打开了。
如果直接可以给这张YUV图加上BMP格式的文件头,就可以直接使用标准的图像处理软件/ACDSee5.0打开了!
这样就会给调试过程中带来【极大的】方便!

公司不方便找到4K显示器,于是用家里的电脑截屏了一张BMP的4K/3840x2160分辨率的BMP图。

画图应用程序的原始/默认分辨率是你的电脑的主显示器的分辨率。
比如我用的笔记本电脑是1080p/1k,也就是1920x1080了!


好久没有碰C/C++了,直接使用python3来完成这项工作了。
首先,使用WIN11自带的画图工具生成一张4K分辨率的BMP图,然后使用python3读取前54个字节。
之后,与BGR3格式的裸图进行组装即可得到正常的4K分辨率的8530的机芯图。


python3的参考源码:
D:\4K>
D:\4K>python
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('test_0.yuv', 'rb') as source_file, open('4K.bmp', 'rb') as temp_file, open('target.bmp', 'wb') as target_file:
...     data = temp_file.read(54)
...     target_file.write(data)
...     data = source_file.read()
...     target_file.write(data)
...
54
24883200
>>>


# 打开源文件和目标文件
with open('test_0.yuv', 'rb') as source_file, open('4K.bmp', 'rb') as temp_file, open('target.bmp', 'wb') as target_file:
    # 读取源文件内容【BMP文件头】
    //data = temp_file.read()
    data = temp_file.read(54)
    # 将内容写入目标文件
    target_file.write(data)
    # 读取源文件内容【BGR3的裸图】
    data = source_file.read()
    # 将内容写入目标文件
    target_file.write(data)


唯一的小小的遗憾:生成的图是倒立的!
百度:BMP图 坐标系

很容易知道BGR3图的坐标系原点在左上角,BMP图的坐标系原点在左下角。
这样你组装后的图片就正好是倒立的!


参开资料:
https://blog.csdn.net/wowocpp/article/details/80805534
windows 坐标系 与 BMP 像素的映射关系


百度:Python读取二进制文件


!!!!
https://www.jb51.net/python/313993top.htm
Python读写二进制文件的示例详解【不能拷贝】
https://blog.csdn.net/m0_59236602/article/details/135714899
Python编程中如何读写二进制文件?
三、实际应用示例
2.复制二进制文件
有时候,我们需要将一个二进制文件复制到另一个文件中。下面是一个复制二进制文件的示例代码:

# 打开源文件和目标文件
with open('source.bin', 'rb') as source_file, open('target.bin', 'wb') as target_file:
    # 读取源文件内容
    data = source_file.read()
    # 将内容写入目标文件
    target_file.write(data)


https://blog.51cto.com/u_14844/11329687
python打开二进制dat文件 python 处理二进制文件 


https://blog.51cto.com/u_16175494/7637992
python读取2进制文件


https://blog.csdn.net/weixin_44609920/article/details/130163005
5.7:Python如何读取二进制文件?


百度:python打印16进制数
python文件之二进制文件的读写
python 二进制读取 前55个字节

!!!!
https://blog.51cto.com/u_16213385/7658233
python 读取二进制文件 部分字节

使用Python读取部分字节
在Python中,我们可以使用open()函数来打开一个二进制文件,并使用read()方法来读取文件的内容。默认情况下,read()方法会一次性读取整个文件的内容。但是我们可以通过指定读取的字节数来读取文件的部分内容。

下面是一个简单的示例代码,演示了如何读取二进制文件的部分字节:

with open('file.bin', 'rb') as f:
    # 读取前100个字节
    data = f.read(100)
    print(data)


百度:PYTHON格式化输出f


https://blog.csdn.net/pumpkin84514/article/details/140628581
python——格式化输出


https://www.cnblogs.com/fat39/p/7159881.html
python基础_格式化输出(%用法和format用法)


百度:python 循环
https://www.jb51.net/article/45864.htm
Python中for循环详解

>>> for i in range(5):             (1)
...     print i
0
1
2
3
4

D:\4K>python
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('4k.bmp', 'rb') as source_file:
...     data = source_file.read(54)
...     print(data)

...
b'BM6\xb0{\x01\x00\x00\x00\x006\x00\x00\x00(\x00\x00\x00\x00\x0f\x00\x00p\x08\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\xb0{\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> print(data[0])
66
>>> print('0x%02d', data[0])
0x%02d 66
>>> print('0x%02d'% data[0])
0x66

>>> for i in range(54):
...     print('0x%02x, '% data[i])

...
0x42,
0x4d,
0x36,
0xb0,
0x7b,
0x01,
0x00,
0x00,
0x00,
0x00,
0x36,
0x00,
0x00,
0x00,
0x28,
0x00,
0x00,
0x00,
0x00,
0x0f,
0x00,
0x00,
0x70,
0x08,
0x00,
0x00,
0x01,
0x00,
0x18,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xb0,
0x7b,
0x01,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
>>> exit()

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值