python实现颜色空间转换程序(Tkinter)

yipeiwu_com5年前Python基础

本文主要基于colorsys实现,样例是从hls转换到rgb,如果要换颜色空间很容易只需要修改一个函数,具体内容如下

用到了Scale和Canvas组件

运行效果图:

代码如下:

from Tkinter import * 
import colorsys 
#操作后的响应函数 
def update(* args): 
  'color' 
  r,g,b = colorsys.hls_to_rgb(h.get() / 255.0, l.get() / 255.0, s.get() / 255.0) 
  r,g,b = r * 255, g * 255, b * 255 
  rgb.configure(text = 'RGB:(%d, %d, %d)' % (r, g, b)) 
  c.configure(bg = '#%02X%02X%02X' %(r, g, b)) 
 
root = Tk() 
hue = Label(root, text = 'Hue') 
hue.grid(row = 0, column = 0) 
 
light = Label(root, text = 'Lightness') 
light.grid(row = 0, column = 1) 
 
sat = Label(root, text = 'Saturation') 
sat.grid(row = 0, column = 2) 
#初始化颜色为rgb的000,也就是纯黑色 
rgb = Label(root, text = 'RGB(0, 0, 0)') 
rgb.grid(row = 0, column = 3) 
 
 
h = Scale(root, from_ = 255, to = 0, command = update) 
h.grid(row = 1, column = 0) 
 
l = Scale(root, from_ = 255, to = 0, command = update) 
l.grid(row = 1, column = 1) 
 
s = Scale(root, from_ = 255, to = 0, command = update) 
s.grid(row = 1, column = 2) 
 
c = Canvas(root, width = 100, height = 100, bg = 'Black') 
c.grid(row = 1, column = 3) 
 
root.mainloop() 

以上就是本文的全部内容,希望对大家的学习Python程序设计有所帮助。

相关文章

常用python编程模板汇总

在我们编程时,有一些代码是固定的,例如Socket连接的代码,读取文件内容的代码,一般情况下我都是到网上搜一下然后直接粘贴下来改一改,当然如果你能自己记住所有的代码那更厉害,但是自己写毕...

对python打乱数据集中X,y标签对的方法详解

对python打乱数据集中X,y标签对的方法详解

今天踩过的两个小坑: 一.用random的shuffle打乱数据集中的数据-标签对 index=[i for i in range(len(X_batch))] # print(ty...

利用Psyco提升Python运行速度

Psyco 是严格地在 Python 运行时进行操作的。也就是说,Python 源代码是通过 python 命令编译成字节码的,所用的方式和以前完全相同(除了为调用 Psyco 而添加的...

Python制作刷网页流量工具

准备 必须环境: Python3 开始 先实现一个简单的版本,直接上代码: import urllib.request import urllib.error #创建get方法...

python 基于dlib库的人脸检测的实现

python 基于dlib库的人脸检测的实现

本周暂时比较清闲,可以保持每日一更的速度。 国外身份证项目新增需求,检测出身份证正面的人脸。最开始考虑mobilenet-ssd,经同事提醒,有现成的人脸库dlib,那就用传统方法尝试一...