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()  

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM