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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

win10安装tensorflow-gpu1.8.0详细完整步骤

win10安装tensorflow-gpu1.8.0详细完整步骤

在整个安装的过程中也遇到了很多的坑,故此做个记录,争取下次不再犯! 我的整个基本配置如下: 电脑环境如下:win10(64位)+CPU:E5-2603 +GPU:GTX 1070 需要安...

python实现壁纸批量下载代码实例

python实现壁纸批量下载代码实例

项目地址:https://github.com/jrainlau/wallpaper-downloader 前言 好久没有写文章了,因为最近都在适应新的岗位,以及利用闲暇时间学习pyth...

Python 、Pycharm、Anaconda三者的区别与联系、安装过程及注意事项

Python 、Pycharm、Anaconda三者的区别与联系、安装过程及注意事项

1、致欢迎词 我将详细讲述在学Python初期的各种手忙脚乱的问题的解决,通过这些步骤的操作,让你的注意力集中在Python的语法上以及后面利用Python所解决的项目问题上。而我自己作...

Python 的AES加密与解密实现

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代...

对Python的zip函数妙用,旋转矩阵详解

Python的zip函数 示例1: x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x, y, z) print xy...