什么是RGB565参考这个
https://blog.csdn.net/byhook/article/details/84262330

#!/usr/bin/python
import os,sys,struct
from PIL import Image
# 图片(jpg/png)转ARGB1555
def main():
	infile = "1.png"
	outfile = "res.bin"
	im=Image.open(infile)
	im.show()
	print("read %s\nImage Width:%d Height:%d" % (infile, im.size[0], im.size[1]))
	f = open(outfile, "wb")
	pix = im.load()  #load pixel array
	for h in range(im.size[1]):
		for w in range(im.size[0]):
			R = pix[w, h][0] >> 3
			G = pix[w, h][1] >> 3
			B = pix[w, h][2] >> 3
			# argb第一位要是1,才是不透明,是0则是全透明
			argb = (1 << 15) | (R << 10) | (G << 5) | B
			# 转换的图是小端的,所以先后半字节,再前半字节
			f.write(struct.pack('B', argb & 255))
			f.write(struct.pack('B', (argb >> 8) & 255))
	f.close()
	print("write to %s" % outfile)
if __name__ == "__main__":
	sys.exit(main())

#!/usr/bin/python
import os,sys,struct
from PIL import Image

# 图片(jpg/png)转RGB565
def main():
	infile = "1.jpg"
	outfile = "res.bin"
	im=Image.open(infile)
	im.show()
	print("read %s\nImage Width:%d Height:%d" % (infile, im.size[0], im.size[1]))

	f = open(outfile, "wb")
	pix = im.load()  #load pixel array
	for h in range(im.size[1]):
		for w in range(im.size[0]):
			R = pix[w, h][0] >> 3
			G = pix[w, h][1] >> 2
			B = pix[w, h][2] >> 3
			rgb = (R << 11) | (G << 5) | B
			# 转换的图是小端的,所以先后半字节,再前半字节
			f.write(struct.pack('B', rgb & 255))
			f.write(struct.pack('B', (rgb >> 8) & 255))

	f.close()
	print("write to %s" % outfile)

if __name__ == "__main__":
	sys.exit(main())

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐