You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PNG图片转换为static const uint8_t类型数组?

解决ImageMagick转PNG为static const uint8_t数组的问题

你需要调整ImageMagick命令的参数顺序和定义,才能生成符合要求的static const uint8_t数组,同时控制每行元素数量来匹配现有icons.h的格式(约70行),具体操作如下:

正确的命令示例

convert /home/XXX/TempStuff/Arrow-circle-down.png -resize 64x64! -colorspace Gray -depth 8 -define h:format=gray -define h:type="static const uint8_t" -define h:name=arrow_circle_down_icon -define h:rows-per-line=58 /home/XXX/TempStuff/grey64x64.h

参数说明

  • -resize 64x64!:强制将图像转为64x64尺寸(加!确保严格匹配,避免原图像尺寸不符导致数组长度错误)
  • -colorspace Gray:将图像转为灰度图,和你指定的h:format=gray对应
  • -depth 8:确保每个像素用8位(即uint8_t的范围0-255)存储
  • -define h:type="static const uint8_t":直接指定数组的类型为你需要的static const uint8_t
  • -define h:name=arrow_circle_down_icon:自定义数组的名称,可根据图标修改
  • -define h:rows-per-line=58:控制每行输出58个元素,64×64=4096个元素,4096÷58≈70.6,最终生成约70行的数组,和现有库格式一致

为什么之前的命令出错?

你之前的命令把-size 64x64放在了输出路径前,这个参数用于创建新图像时指定尺寸,不适合处理现有图像,导致ImageMagick没有正确裁剪/缩放图像;加上没有指定数组类型和每行元素数,才生成了超长的unsigned char数组。

内容的提问来源于stack exchange,提问作者user2343618

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 09:32:12