当前位置:网站首页>Convert PNG with transparency to C array
Convert PNG with transparency to C array
2022-07-19 06:32:00 【xp5xp6】
import matplotlib.pyplot as plt # plt Used to display pictures
import numpy as np
import cv2
# use cv2.imread Read grayscale , It is found that the picture obtained is 3 passageway , Upon verification, it is found that ,cv2.imread() The function reads a color picture by default , Want to read the grayscale image , You need to set parameters .
#IndexError: index 3 is out of bounds for axis 0 with size 3 There is no such mistake alpha passageway c Array is wide x tall 0xff
img = cv2.imread("d:/2626.png", cv2.IMREAD_UNCHANGED)
h, w, g = (img.shape)
print(h)
f = open("d:/2626_alpha.txt", 'wb')
a = np.array(img)
print(np.shape(img));
# print(img)
print(img[0][0][0])
f.write("{".encode())
for j in range(0, h):
for i in range(0, w):
b = a[j][i][3]
print(type(hex(b)))
str = hex(b) + ','
f.write(str.encode())
f.write("};".encode())
f.close()
plt.imshow(img) # display picture
plt.axis('on') # No axes are displayed
plt.show()import matplotlib.pyplot as plt # plt Used to display pictures
import numpy as np
import cv2
# use cv2.imread Read grayscale , It is found that the picture obtained is 3 passageway , Upon verification, it is found that ,cv2.imread() The function reads a color picture by default , Want to read the grayscale image , You need to set parameters .
#flags: Sign to read in picture
#cv2.IMREAD_COLOR: Default parameters , Read in a colour picture , Ignore alpha passageway
#cv2.IMREAD_GRAYSCALE: Read in the grayscale image
#cv2.IMREAD_UNCHANGED: seeing the name of a thing one thinks of its function , Read the full picture , Include alpha passageway
def rgb888_to_rgb565(r, g, b):
aa = ((g >> 5) & 0x07)
bb = ((r >> 3) & 0x1F) << 3
cc = ((b >> 3) & 0x1F) << 8
dd = ((g >> 2) & 0x07) << 13
print(hex(aa))
print(hex(bb))
print(hex(cc))
print(hex(dd))
c = aa | bb | cc | dd
h = (c & 0xFF00) >> 8
l = (c & 0x00FF)
c = (l << 8) | (h)
return np.uint16(c)
# opencv Read the picture
path = "D:\\"
img = cv2.imread("D:\\2626.png", cv2.IMREAD_UNCHANGED)
plt.imshow(img) # display picture
# Get image information
h, w, g = (img.shape)
print(h)
f = open(path + "2626_pixel.txt", 'w')
a = np.array(img)
print(np.shape(img))
# print(img)
print(img[0][0][0])
f.write("{")
for j in range(0, h):
for i in range(0, w):
b = a[j][i][0]
g = a[j][i][1]
r = a[j][i][2]
rgb565 = rgb888_to_rgb565(r, g, b);
rgb565_h = rgb565 >> 8
rgb565_l = rgb565 & 0xFF
ap = a[j][i][3]
print(hex(r), hex(g), hex(b), hex(rgb565))
f.write(str(hex(rgb565)) + ",")
#f.write(str(hex(rgb565_h)) + ",")
#f.write(str(hex(rgb565_l)) + ",")
#f.write(str(hex(ap)) + ",")
# f.write(str(hex(r))+","+str(hex(g))+","+str(hex(b))+","+str(hex(ap))+",")
# f.write('\n')
f.write("};")
f.close()
plt.axis('on') # No axes are displayed
plt.show()
边栏推荐
猜你喜欢

Positional Change of the Eyeball During Eye Movements: Evidence of Translatory Movement眼球运动过程中眼球的位

【力扣】环形链表 II

面试复习第N次

Learning video saliency from human gaze using candidate selection

三维凝视估计,没有明确的个人校准2018

Interview review nth time

虚拟现实中的眼睛跟踪

2022/07/10 group 5 Ding Shuai's study notes day03

感知智能手机上用户的关注状态

Positional change of the eyeball during eye movements: evidence of translational movement
随机推荐
Busybox date date increases by one day, and decreases by one day on the Internet tomorrow
In Chapter 5, can we directly call the run () method to start a new thread
What's the worst programmer you've ever seen?
Ehab the xorcist (XOR property, construction)
Leetcode tree
你见过的最差的程序员是怎样的?
你的企业最适合哪种深度学习?
busybox 指定日期修改 暂时不需要clock -w 写入硬件
嵌入式C语言volatile作用
Where have all the older programmers gone?
通过VOR深度估计解决三维注视交互中的目标模糊问题
Longest bracket match (linear DP)
Preorder traversal of binary tree
2022/07/12 学习笔记 (day05)循环
Unity2d learning Fox game production process 1: basic game character control, animation effects, lens control, item collection, bug optimization
量子三体问题: 数值计算概述
Internship written examination answers
感知智能手机上用户的关注状态
LeetCode树
Make config analysis of configuration commands before uboot compilation