|
|
我们有个300*300像素的图片通过两个步骤在表端显示
一·、图片转像素
input_bmp = 'C:/Users/15085/Desktop/img/111.bmp'
output_bin = 'C:/Users/15085/Desktop/img/111.txt'
from PIL import Image
import numpy as np
try:
# 打开图像并转换为 RGB 模式
image = Image.open(input_bmp).convert('RGB')
print(image)
print(image.size)
# 将图像转换为 NumPy 数组
images = np.asarray(image)
# 获取图像的高度和宽度
height, width, _ = images.shape
# 打开文件以写入 RGB565 数据
with open(output_bin, 'w') as f:
# 写入数组声明
f.write(f"const unsigned short image_rgb565[{height}][{width}] = {{\n")
for y in range(height):
f.write(" {")
for x in range(width):
# 获取当前像素的 RGB 值
r, g, b = images[y, x]
# 将 RGB 值转换为 RGB565 格式
r5 = int((r >> 3) & 0x1F) # 取红色的高 5 位
g6 = int((g >> 2) & 0x3F) # 取绿色的高 6 位
b5 = int((b >> 3) & 0x1F) # 取蓝色的高 5 位
# 组合成 16 位的 RGB565 值
rgb565 = int((r5 << 11) | (g6 << 5) | b5)
f.write(f"0x{rgb565:04X}")
if x < width - 1:
f.write(", ")
f.write("}")
if y < height - 1:
f.write(",\n")
else:
f.write("\n")
f.write("};\n")
print("RGB565 数据已保存到 rgb565_array.c")
except FileNotFoundError:
print("文件未找到,请检查文件路径是否正确。")
except Exception as e:
print(f"发生了其他错误: {e}")
二、uikit 显示
void setupUi()
{
// Add ImageView to App
if (image1_ == nullptr) {
image1_ = new UIImageView();
views[0] = image1_;
}
image1_->SetPosition(67, 67);
image1_->Resize(300, 300);
image1_->SetViewId("image1");
image1_->SetVisible(true);
///////////////////////////////////////////
uint16_t imgWidth = 300;
uint16_t imgHeight = 300;
uint16_t alignedImgWidth = ALIGN_BYTE(imgWidth, BYTE_ALIGNMENT);
uint32_t rgbSize = alignedImgWidth * imgHeight * 2;
ImageInfo tempImgInfo = {0};
tempImgInfo.color = 0;
tempImgInfo.dataSize = rgbSize;
tempImgInfo.header.width = imgWidth;
tempImgInfo.header.height = imgHeight;
tempImgInfo.header.colorMode = RGB565;
tempImgInfo.header.compressMode = 0;
uint8_t* rgbAddr = reinterpret_cast<uint8_t*>(ImageCacheMalloc(tempImgInfo));
for (uint16_t i = 0; i < imgHeight; i++) {
uint8_t* rowStart = rgbAddr + i * alignedImgWidth * 2;
for (uint16_t j = 0; j < imgWidth; j++) {
uint16_t* temp = (uint16_t*)(rowStart + j * 2);
*temp = image_rgb565[j];
}
}
image1_->SetSrc(&tempImgInfo);
///////////////////////////////////////////
}
|
|