数码之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

微信登录

微信扫一扫,快速登录

搜索
查看: 4311|回复: 5

[C51] [micropython_python]读单色bmp颜色点阵,生成数组

[复制链接]
发表于 2020-7-15 15:09:31 | 显示全部楼层 |阅读模式
先上图片版本



单色、宽度是8的倍数
bmp颜色点阵,生成数组定义和文本图像文件。
(红色字,是局限性说明)
按 bmp 格式、二进制读取,所以,很方便转成别的编程语言。至于代码里面的旋转、缩放,就看需要了。



原图


缩放、旋转180



生成的文本图像

// image size: 128 x 67
code unsigned char bmp[67][16]={{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe0, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},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xff, 0xff, 0xff, 0x80, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdf, 0x80, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xa0, 0x40, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x8, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xe0, 0xc0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe6, 0xe0, 0x80, 0x0, 0x0},
{0x0, 0x0, 0x0, 0xf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x60, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x5, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x70, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x60, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x30, 0x37, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc8, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x18, 0x4, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xee, 0xc8, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x2, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xec, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x1f, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf7, 0xbd, 0x4, 0x0, 0x0, 0x0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0, 0x4, 0x8f, 0x3f, 0xb7, 0xf5, 0x21, 0x21, 0xc0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x4, 0x1e, 0x99, 0xcd, 0xff, 0xa1, 0xe8, 0x1e, 0xf0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x5, 0x3a, 0x18, 0x4d, 0x5b, 0x39, 0x5c, 0x1c, 0x4, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x1, 0xc, 0x0, 0x4c, 0x71, 0x3c, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0xc3, 0x89, 0xcf, 0x78, 0xb0, 0x0, 0x0, 0x20, 0x40, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0xd9, 0x3c, 0x63, 0xd9, 0xa0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0xc3, 0xf4, 0x70, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xde, 0x40, 0xdb, 0x4e, 0x80, 0x20, 0x0, 0x0, 0x0, 0x0},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xfe, 0xc8, 0x17, 0xd7, 0xc0, 0x20, 0x0, 0x0, 0x0, 0x0}
};

生成的C语言 数组定义



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

x

打赏

参与人数 1家元 +30 收起 理由
家睦 + 30

查看全部打赏

发表于 2020-7-15 15:30:41 | 显示全部楼层
哈哈,完全看不出是什么啊:cool:
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-15 17:08:14 | 显示全部楼层
  1. # -*- coding:utf-8 -*-
  2. #
  3. # 测试 Python 3.7.4 、Windows7 X64
  4. # DEVCANG
  5. #
  6. import os, math
  7. # PIL 库,如果不转换成单色bmp,可以不要此库,注释掉引用和函数
  8. from PIL import Image

  9. iWidth  =128   # 缩放,一定是8的倍数。。。。。按12864屏,最好成比例
  10. iRotate =0     # 旋转180度,方便后边直接生成图片点阵   
  11. mirrorH =1     # 旋转180度,方便后边直接生成图片点阵   
  12. DEBUG   =False
  13. def toBinaryBmp(finput, foutput): # 图片二值化(转换成单色bmp)(按比例缩小到宽度128点)
  14.     if not os.path.exists(finput):
  15.         print(finput," is NOT exist.")
  16.         return
  17.    
  18.     img = Image.open(finput).convert('RGBA');
  19.     w,h = img.size
  20.    
  21.     # 模式L”为灰色图像,它的每个像素用8个bit表示,0表示黑,255表示白,其他数字表示不同的灰度。
  22.     if(iRotate==0):
  23.         if(mirrorH==0):
  24.             Img = img.resize((iWidth, int(h/w*iWidth)),Image.ANTIALIAS).convert('L')
  25.         else:
  26.             Img = img.transpose(Image.FLIP_TOP_BOTTOM).resize((iWidth, int(h/w*iWidth)),Image.ANTIALIAS).convert('L')
  27.     else:
  28.         if(mirrorH==0):
  29.             Img = img.rotate(iRotate).resize((iWidth, int(h/w*iWidth)),Image.ANTIALIAS).convert('L')
  30.         else:
  31.             Img = img.rotate(iRotate).transpose(Image.FLIP_TOP_BOTTOM).resize((iWidth, int(h/w*iWidth)),Image.ANTIALIAS).convert('L')
  32.     # Img.save("test1.jpg")
  33.    
  34.     # 自定义灰度界限,大于这个值为黑色,小于这个值为白色
  35.     threshold = 198 #200
  36.       
  37.     table = []
  38.     for i in range(256):
  39.       if i < threshold:
  40.         table.append(0)
  41.       else:
  42.         table.append(1)
  43.       
  44.     photo = Img.point(table, '1')    # 图片二值化
  45.     photo.save(foutput)   


  46. def toInt(bb, start):
  47.     return (bb[start+1]<<8)|bb[start]
  48. def toLong(bb, start):
  49.     return (bb[start+3]<<24)|(bb[start+2]<<16)|(bb[start+1]<<8)|bb[start]


  50. # RAW 方式读单色bmp,所以,很方便转成别的编程语言

  51. # 单色bmp读取点阵、生成文本图像(180度倒转的)
  52. def toBitCharts(finput, foutput):
  53.     if not os.path.exists(finput):
  54.         print(finput," is NOT exist.")
  55.         return
  56.         
  57.     fbmp=open(finput,'rb')
  58.     bb=fbmp.read(14)
  59.     if (bb==None) or (len(bb)<14):
  60.         print(finput," bmp HEAD error.")
  61.         return

  62.     ftype  = hex(toInt(bb, 0)) #
  63.     ttSize = toLong(bb, 2)
  64.     bOff   = toLong(bb, 10)
  65.     if DEBUG:
  66.         print("bfType          =",          ftype) #WORD  bfType
  67.         print("bfSize          =",         ttSize) #DWORD bfSize 指定文件大小,包括这14个字节
  68.         print("bfReserved1     =", toInt(bb,   6)) #WORD  bfReserved1
  69.         print("bfReserved2     =", toInt(bb,   8)) #WORD  bfReserved2
  70.         print("bfOffBits       =",           bOff) #DWORD bfOffBits 从文件头到实际的位图数据的偏移字节数,即图3中前三个部分的长度之和

  71.     print()
  72.     bb       = fbmp.read(40)
  73.     if (bb==None) or (len(bb)<40):
  74.         print(finput," bmp struct HEAD error.")
  75.         return
  76.     bmpW     = toLong(bb,   4)
  77.     bmpH     = toLong(bb,   8)
  78.     bitCount = toInt (bb,  14)
  79.     imgSize  = toLong(bb,  20)
  80.     if DEBUG:
  81.         print("bfSize          =",toLong(bb,   0)) #DWORD biSize
  82.         print("biWidth         =",           bmpW) #LONG  biWidth
  83.         print("biHeight        =",           bmpH) #LONG  biHeight
  84.         print("biPlanes        =",toInt (bb,  12)) #WORD  biPlanes  1
  85.         print("biBitCount      =",       bitCount) #WORD  biBitCount
  86.         print("biCompression   =",toLong(bb,  16)) #DWORD biCompression
  87.         print("biSizeImage     =",        imgSize) #DWORD biSizeImage
  88.         print("biXPelsPerMeter =",toLong(bb,  24)) #LONG  biXPelsPerMeter
  89.         print("biYPelsPerMeter =",toLong(bb,  28)) #LONG  biYPelsPerMeter
  90.         print("biClrUsed       =",toLong(bb,  32)) #DWORD biClrUsed
  91.         print("biClrImportant  =",toLong(bb,  36)) #DWORD  biClrImportant
  92.    
  93.     if(bOff!=62) or (bitCount!=1) or (imgSize+bOff!=ttSize):
  94.         print("The file is NOT binary bmp image")
  95.         return
  96.     # bb=fbmp.read(8)       #调色板,没使用,按0/1
  97.     # for i in range(0, 8):
  98.     #     print(hex(bb[i]), end=" ")
  99.     # print()

  100.     mRow = bmpW/8              # 每行用多少字节(可能不是整数的)
  101.     mOf8 = math.ceil(mRow)     # 取整
  102.     mOf4 = math.ceil(mRow/4)*4 # 每行的字节是必须是4的倍数,不是则补0了
  103.     mEx = mOf4 - mOf8          # 补了多少个0(字节)
  104.    
  105.     if((mOf4*bmpH!=imgSize) or(mRow!=mOf8)):
  106.         print("The file content length except")
  107.         return

  108.     fbmp.seek(bOff, 0)         # SEEK_SET  跳转到bmp内容
  109.     if DEBUG:
  110.         print("tell: ", fbmp.tell(),", mEx: ", mEx,"\r\n")
  111.     print("// image size: %d x %d" % (bmpW, bmpH))
  112.     print("code unsigned char bmp[%d][16]={{" % bmpH)

  113.     cols=0
  114.     lines=0

  115.     p1 = "#".encode()          # 黑色点
  116.     p2 = " ".encode()          # 白色点
  117.     p3 = "\r\n".encode()
  118.     fOut=open(foutput,'wb')
  119.     while True:
  120.        bb=fbmp.read(1)
  121.        if (bb==None) or (len(bb)<1): break
  122.        b1=bb[0]
  123.        print(hex(b1), end="")
  124.        for i in range(0, 8):
  125.            if((b1&0x80)==0x80):  # 高位在前,先输出高位
  126.            # if((b1&1)==1):      # 低位在前,先输出低位
  127.                fOut.write(p2)    # 白铁点
  128.            else:
  129.                fOut.write(p1)    # 黑色点
  130.            b1 = b1 << 1
  131.        cols=cols+1
  132.        if(cols==mRow):
  133.           lines=lines+1
  134.           cols=0
  135.           fOut.write(p3)    # 一行结束,回车换行
  136.           if(lines==bmpH):
  137.               print("}")
  138.               break
  139.           print("}, \r\n{", end="")
  140.        else:
  141.            print(", ", end="")

  142.     fOut.write(p3)
  143.     fOut.close()
  144.     fbmp.close()
  145.     print("};")
  146.    
  147. toBinaryBmp("Football.jpg", "Football_1bit.bmp")
  148. toBitCharts("Football_1bit.bmp", "Football_1bit.bmp.txt")

复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-15 17:09:51 | 显示全部楼层
人艰不拆了 发表于 2020-7-15 15:30
哈哈,完全看不出是什么啊

文本显示的横竖长度不相同,所以变形较大,要是相同的长度,会有个形状看到
回复 支持 反对

使用道具 举报

发表于 2020-7-16 21:31:21 | 显示全部楼层
看到了熟悉的代码,Python队伍逐渐壮大中.........:victory:
回复 支持 反对

使用道具 举报

发表于 2020-7-17 21:42:30 | 显示全部楼层
果然还是需要这种彩图直接转数组的代码:lol:
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 微信登录

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2025-7-11 02:44 , Processed in 0.218400 second(s), 13 queries , Redis On.

Powered by Discuz!

© 2006-2025 MyDigit.Net

快速回复 返回顶部 返回列表