带透明png转换成c数组

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()




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值