RGB888转换为RGB565原理及工具

news/2025/3/17 21:02:45/

1.原理
首先RGB的范围是0-255,包括RGB三个值。

第一步将R,G,B三个值分别转化为八位二进制数,这里以GREEN为例子。
GREEN R:0 G:255 B:0
二进制:R00000000 11111111 00000000

565的意思就是RGB的位数,R取5位,G取六位,B取5位。其中要求取高位,舍低位。

00000 000 11111111 00000 00

其中非粗体的 0和1 舍去

得到0000011111100000
再转化为十六进制数0x7e0

2.工具

#!/usr/bin/env python
# -*- coding: UTF-8 -*-from tkinter import *
import rewindow = Tk()
window.title("RGB888 转换 RGB565")
window.geometry('450x300+300+250')def scalecommand(color):#print(s1.get(),s2.get(),s3.get())b=s3.get()g=s2.get()r=s1.get()R = r & 0xF8G = g & 0xFcB = b & 0xF8rgb565 = (R << 8) | (G << 3) | (B >> 3)rgb888 = (r << 16) | (g << 8) | bR = R >> 3G = G >> 2B = B >> 3rgb888_text = 'RGB888: '+ "#%06x"%rgb888rgb888_Label.configure(text=rgb888_text)rgb565_text = '565: '+ "%d " %R + "%d " %G + "%d" %Brgb565_Label.configure(text=rgb565_text)#print("%#08X"%rgb)info_Label.configure(bg="#%06x" %rgb888)def HexToDec(value):try:return int(value, 16)except ValueError:return "Invalid Hexadecimal Value"def buttonClick1():# RGB888 转 RGB565try:c888 = int(rgb1_Entry.get(),16)         #字符转16进制整数except ValueError:print( "Invalid Hexadecimal Value")returnrgb2_Entry.delete(0,END)if c888 == None :returnelse:b = (c888 & 0xFF)                 #转换RB 取得rgb颜色Bg = int((c888 & 0xFF00) >> 8)         #转换G 取得rgb颜色Gr = int((c888 & 0xFF0000) >>16)     #转换R 取得rgb颜色RR = r & 0xF8                        #取得RGB565的5位RG = g & 0xFc                        #取得RGB565的6位GB = b & 0xF8                        #取得RGB565的5位Brgb565 = (R << 8) | (G << 3) | (B >> 3)                               #print("%#06x" %rgb565)               #设置滑块位置s1.set(r)               s2.set(g)s3.set(b)#显示RGB888和RGB565颜色码info_Label.configure(bg="#%06x" %c888)      rgb888_text = 'RGB888: '+ "#%06x"%c888rgb888_Label.configure(text=rgb888_text)rgb565_text = 'RGB565: '+ "%#06x" %rgb565rgb565_Label.configure(text=rgb565_text)def buttonClick2():# RGB565 转 RGB888rgb1_Entry.delete(0,END)try:c565 = int(rgb2_Entry.get(),16)except ValueError:print( "Invalid Hexadecimal Value")returnif c565 == None :returnelse:b = (c565 & 0x001F)                 #转换R g = int((c565 & 0x07E0))           #转换G r = int((c565 & 0xF800))          #转换BR = r >> 8G = g >> 3B = b << 3rgb888 = (R << 16) | (G << 8) | B        #print("%#06x" %rgb888)s1.set(R)s2.set(G)s3.set(B)info_Label.configure(bg="#%06x" %rgb888)rgb888_text = 'RGB888: '+ "#%06x"%rgb888rgb888_Label.configure(text=rgb888_text)rgb565_text = 'RGB565: '+ "%#06x" %c565rgb565_Label.configure(text=rgb565_text)Rgb1_Label = Label(window, text="RGB888 代码:",height = 2,fg='#191970')
Rgb1_Label.place( x =20, y = 25 , anchor=NW)R1_Label = Label(window, text="#",height = 2,fg='#191970')
R1_Label.place( x =20, y = 50 , anchor=NW)
rgb1_Entry = Entry(window,width=10)
rgb1_Entry.place( x =40, y = 60 , anchor=NW)Rgb2_Label = Label(window, text="RGB565 代码:",height = 2,fg='#191970')
Rgb2_Label.place( x =20, y = 85 , anchor=NW)R2_Label = Label(window, text="0x",height = 2,fg='#191970')
R2_Label.place( x =20, y = 110 , anchor=NW)
rgb2_Entry = Entry(window,width=10)
rgb2_Entry.place( x =40, y = 120 , anchor=NW)button1 = Button(window,text="转换", bg='#8FBC8F',command=buttonClick1)    #转换按键
button1.place( x =160, y = 40 , anchor=NW)button2 = Button(window,text="转换", bg='#8FBC8F',command=buttonClick2)    #转换按键
button2.place( x =160, y = 110 , anchor=NW)info_Label = Label(window, text="",height = 10,width=20)        #色块
info_Label.configure(bg='#FFFFFF')
info_Label.place( x =280, y = 20 , anchor=NW)R_Label = Label(window, text="R",height = 1,width=1)       
R_Label.place( x =20, y = 170 , anchor=NW)
s1 = Scale(window,  from_=0, to=255, orient=HORIZONTAL,length=200, showvalue=1, tickinterval=0, resolution=1, command=scalecommand)     #滑块R
s1.place( x =40, y = 150 , anchor=NW)G_Label = Label(window, text="G",height = 1,width=1)       
G_Label.place( x =20, y = 210 , anchor=NW)
s2 = Scale(window,  from_=0, to=255, orient=HORIZONTAL,length=200, showvalue=1, tickinterval=0, resolution=1, command=scalecommand)   #滑块G
s2.place( x =40, y = 190 , anchor=NW)B_Label = Label(window, text="B",height = 1,width=1)        
B_Label.place( x =20, y = 250 , anchor=NW)
s3 =  Scale(window, from_=0, to=255, orient=HORIZONTAL,length=200, showvalue=1, tickinterval=0, resolution=1, command=scalecommand)   #滑块B
s3.place( x =40, y = 230 , anchor=NW)rgb888_Label = Label(window, text="RGB888:",height = 1,width=18 , fg = 'blue',anchor="w")
rgb888_Label.place( x =280, y = 210 )rgb565_Label = Label(window, text="565:",height = 1,width=18 , fg = 'blue',anchor="w")  
rgb565_Label.place( x =280, y = 250 ) window.mainloop()

转自 https://blog.csdn.net/wild_lee/article/details/122009234
修改了一下将RGB565的R G B 分别用十进制打出

一些常用的颜色
RGB888和RGB565颜色对照表

RGB颜色值与十六进制颜色码互转

RGB在线颜色生成调色板

色彩中间色/颜色中间值计算


http://www.ppmy.cn/news/197614.html

相关文章

HDMI 调试之输出RGB888

&#xff08;仅作记录&#xff0c;rk3588平台&#xff09; RGB888这种数据格式可以被csi或dsi接收&#xff0c;驱动代码里默认是被dsi接收&#xff0c;但我们经常会遇到hdmi输出rgb888到mipi csi&#xff0c;所以需要在驱动中增加对csi接口的支持。 首先了解下回调函数 v4l2_su…

白光FX-888D 温度初始化 350度

1.先关机 2.同时按 “UP” 和“ENTER” 并开机&#xff0c; 3.当面板显示"A" 时&#xff0c; 4.按下“ENTER” 就进入自我调节到350度

解决——imread.exe 中的 0x757da832 处有未经处理的异常: Microsoft C++ 异常: 内存位置 0x0052f888 处的 cv::Exception。

出现上图错误提示&#xff0c;问题基本是图片路径的问题。应将程序相应的图像放置在工程目录下&#xff08;和cpp源文件同一目录下&#xff09;。

0x757da832 处有未经处理的异常: Microsoft C++ 异常: 内存位置 0x0052f888 处的 cv::Exception。

opencv新手&#xff0c;没把图片放到相应的目录里面&#xff0c;读不到图片。把将被处理的图片放到与含有main函数的cpp文件相同的目录中。

JSBridge 原理

JSBridge 的起源 近些年&#xff0c;移动端普及化越来越高&#xff0c;开发过程中选用 Native 还是 H5 一直是热门话题。Native 和 H5 都有着各自的优缺点&#xff0c;为了满足业务的需要&#xff0c;公司实际项目的开发过程中往往会融合两者进行 Hybrid 开发。Native 和 H5 分…

旧改快讯--桑泰南山桃源“工改商住”项目规划修改

南山区桃源街道西丽同富裕工业城城市更新单元原列入《2019年深圳市南山区城市更新单元计划第一批计划》&#xff0c;后进行更新方向调整&#xff0c;列入《2020年深圳市南山区城市更新单元计划第三批计划》&#xff0c;2022年8月发布实施主体公示&#xff0c;实施主体为深圳市桑…

oracle数据文件恢复步骤

1、基于linux操作系统文件恢复 条件&#xff1a;1、误强制删除linux下的数据文件&#xff08;rm -rf&#xff09;。2、未重启数据库或操作系统。3、数据库是归档模式 恢复原理&#xff1a;句柄恢复文件–因为我们的操作系统是linux&#xff0c;当数据文件从操作系统级别被rm掉…

1. 爬虫及爬虫的步骤

1. 爬虫及爬虫的步骤 文章目录 1. 爬虫及爬虫的步骤1. 爬虫是什么&#xff1f;2. 爬虫的作用3. 爬虫步骤3.1 获取网页3.2 解析网页3.3 存储数据 4. 总结 1. 爬虫是什么&#xff1f; 爬虫就是写一段代码让计算机模仿人类自动访问网站。 2. 爬虫的作用 爬虫可以代替人们自动地…