当前位置:网站首页>带透明png转换成c数组
带透明png转换成c数组
2022-07-17 05:12:00 【xp5xp6】
import matplotlib.pyplot as plt # plt 用于显示图片
import numpy as np
import cv2
#用cv2.imread读取灰度图,发现获得的图片为3通道,经查证发现,cv2.imread()函数默认读取的是一副彩色图片,想要读取灰度图,则需要设置参数。
#IndexError: index 3 is out of bounds for axis 0 with size 3 出现这种错误是没有alpha通道 c数组为宽x高个0xff
img = cv2.imread("d:/2626.png", cv2.IMREAD_UNCHANGED)
h, w, g = (img.shape)
print(h)
f = open("d:/2626_alpha.txt", 'wb')
a = np.array(img)
print(np.shape(img));
# print(img)
print(img[0][0][0])
f.write("{".encode())
for j in range(0, h):
for i in range(0, w):
b = a[j][i][3]
print(type(hex(b)))
str = hex(b) + ','
f.write(str.encode())
f.write("};".encode())
f.close()
plt.imshow(img) # 显示图片
plt.axis('on') # 不显示坐标轴
plt.show()import matplotlib.pyplot as plt # plt 用于显示图片
import numpy as np
import cv2
#用cv2.imread读取灰度图,发现获得的图片为3通道,经查证发现,cv2.imread()函数默认读取的是一副彩色图片,想要读取灰度图,则需要设置参数。
#flags:读入图片的标志
#cv2.IMREAD_COLOR:默认参数,读入一副彩色图片,忽略alpha通道
#cv2.IMREAD_GRAYSCALE:读入灰度图片
#cv2.IMREAD_UNCHANGED:顾名思义,读入完整图片,包括alpha通道
def rgb888_to_rgb565(r, g, b):
aa = ((g >> 5) & 0x07)
bb = ((r >> 3) & 0x1F) << 3
cc = ((b >> 3) & 0x1F) << 8
dd = ((g >> 2) & 0x07) << 13
print(hex(aa))
print(hex(bb))
print(hex(cc))
print(hex(dd))
c = aa | bb | cc | dd
h = (c & 0xFF00) >> 8
l = (c & 0x00FF)
c = (l << 8) | (h)
return np.uint16(c)
# opencv 读取图片
path = "D:\\"
img = cv2.imread("D:\\2626.png", cv2.IMREAD_UNCHANGED)
plt.imshow(img) # 显示图片
# 获得图像信息
h, w, g = (img.shape)
print(h)
f = open(path + "2626_pixel.txt", 'w')
a = np.array(img)
print(np.shape(img))
# print(img)
print(img[0][0][0])
f.write("{")
for j in range(0, h):
for i in range(0, w):
b = a[j][i][0]
g = a[j][i][1]
r = a[j][i][2]
rgb565 = rgb888_to_rgb565(r, g, b);
rgb565_h = rgb565 >> 8
rgb565_l = rgb565 & 0xFF
ap = a[j][i][3]
print(hex(r), hex(g), hex(b), hex(rgb565))
f.write(str(hex(rgb565)) + ",")
#f.write(str(hex(rgb565_h)) + ",")
#f.write(str(hex(rgb565_l)) + ",")
#f.write(str(hex(ap)) + ",")
# f.write(str(hex(r))+","+str(hex(g))+","+str(hex(b))+","+str(hex(ap))+",")
# f.write('\n')
f.write("};")
f.close()
plt.axis('on') # 不显示坐标轴
plt.show()
边栏推荐
- Hm8203 linear two string charging management controller IC
- Isolate 4-20mA or 0-20mA signal transmission
- 4路编码器脉冲计数器,8路DO,Modbus TCP模块
- 3.7V锂电池升压到5V1A,FS2114升压转换芯片设计布局
- Low power LDO linear regulator IC
- FS4061A(5V USB输入、双节锂电池串联应用、5v升压充电8.4v管理IC
- Antd is not defined
- MCU的最佳存储方案CS创世 SD NAND
- 2021 - 09 - 15
- HM代理商多节锂电充电IC
猜你喜欢

Fs68001 wireless charging SOC chip has simple periphery and schematic diagram of 5W wireless charging scheme

配置VsCode中‘log’快捷键去掉console.log(‘‘);中的分号;

3U VPX导冷高性能SRIO/以太网数据交换板

2019CS品牌SDNAND和eMMC选择对比重要分析

2021-09-15

4 路 FMC+基带信号处理板( 4 路 2G 瞬时带宽 AD+DA)

2021-11-17 ESP32引脚参考

[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....

解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...

2021-11-26 pyautogui 配合雷电模拟器实现手机APP签到答题自动化
随机推荐
CS品牌SDNAND在颜色检测仪行业中的应用案例
TP4054充电IC使用技巧---配合中科蓝讯AB5365B使用
DSL实现自动补全查询
数字信号隔离模块 ADUM1401ARWZ 亚德诺 库存现货
Pressure strain bridge signal processing photoelectric isolation amplifier
2019CS品牌SDNAND和eMMC选择对比重要分析
【CS创世】 SD NAND和Raw NAND优劣势对比分析
FMC子卡:4 通道 250MSPS采样率16 位 AD 采集子卡
MySQL workbench basically uses [create a data table]
4-20mA to 4-20mA 0-5V to 0-5V analog signal isolation transmitter
golang高并发特性goroutine介绍
HRA 1~12W 系列12V《宽压9~18V》转±250VDC等升压变换器
自动补全 & (自定义)拼音分词器 & 搜索时注意事项
Minio installation, deployment and simple use
Boost dc/dc converter
无线充电芯片IC
宽电压输入高电压输出 电压控制型
4 路 FMC+基带信号处理板( 4 路 2G 瞬时带宽 AD+DA)
go语言介绍及应用场景分析
CS品牌SD NAND在空气质量检测行业中的应用案例