• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MKT-porter
博客园    首页    新随笔    联系   管理    订阅  订阅
python将图像向转化为点阵

 

 

转化结果

{{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{237,28,36},{237,28,36},{237,28,36},{0,0,0},{0,0,0},{97,11,15},{237,28,36},{237,28,36},{0,0,0},{0,0,0},{0,0,0},},{{0,0,0},{0,0,0},{0,0,0},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{0,0,0},{0,0,0},},{{0,0,0},{0,0,0},{0,0,0},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{0,0,0},{0,0,0},},{{0,0,0},{0,0,0},{0,0,0},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{0,0,0},{0,0,0},},{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{0,0,0},{0,0,0},},{{0,0,0},{255,242,0},{255,242,0},{0,0,0},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{0,0,0},{0,0,0},{0,0,0},},{{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{237,28,36},{0,0,0},{0,0,0},{0,0,0},{0,0,0},},};

  

 

#encoding:utf-8

#读取图像转化成C++数组
import cv2

imgname="xin.png"
image = cv2.imread(imgname)#读取图像

print(image.shape)  
img_w=16
img_h=8
channels=3
image = cv2.resize(image, (img_w,img_h))  
print(image.shape)

cv2.imshow("Updated",image)#显示图像
cv2.waitKey(1)#程序暂停

(b,g,r) = image[0,0]#读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的


print("位置(0,0)处的像素 - 红:%d,绿:%d,蓝:%d" %(r,g,b))#显示像素值

allmsg="{"
for row in range(int(img_h-1)): #遍历每一列
    
    hangmsg="{"
    for col in range(int(img_w-1)):#遍历每一行 图像  B,G,R  彩灯R G B
        
        pix="{"+str(image[row][col][2])+","+str(image[row][col][1])+","+str(image[row][col][0])+"},"
        hangmsg=hangmsg+pix
    hangmsg=hangmsg+"},"#打包一行
    #print(hangmsg)
    allmsg=allmsg+hangmsg#逐列添加
    
    
allmsg=allmsg+"};"

print(allmsg)


f = open('result.txt','w')
 
f.write(allmsg)




cv2.destroyAllWindows()  

  

posted on 2021-03-28 20:09  MKT-porter  阅读(1381)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3