py 将图片加载为 cpp 数组(保留 alpha 通道)
[編輯] [转简体] (简体译文)
|
作者:huidong
| 分類:【編程】python
[
11 瀏覽
0 評論
2 贊
2 踩
]
概要
正文
import os import cv2 img = cv2.imread("./favorite.png", cv2.IMREAD_UNCHANGED) print("image_shape: ", img.shape) # 图像信息 width = img.shape[0] height = img.shape[1] size = width * height file = open("./output.cpp", "w") file.writelines("DWORD pBuf[" + str(size) + "] = {\n") print(img[0][0]) print(img[0][1]) print(img[0][2]) print(img[0][3]) print(img[0][4]) buf = "" for i in range(0, height): buf += "\t" for j in range(0, width): b = img[i][j][0] g = img[i][j][1] r = img[i][j][2] a = img[i][j][3] value = a << 24 | r << 16 | g << 8 | b value &= 0xffffffff buf += "0x{:08X}, ".format(value) #print("0x{:08X}, ".format(value)) buf += "\n" file.write(buf) file.writelines("};") file.close()
输出十六进制 argb 数组