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音频处理用到的操作的示例代码

python音频处理用到的操作的示例代码

前言 本文主要记录python下音频常用的操作,以.wav格式文件为例。其实网上有很多现成的音频工具包,如果仅仅调用,工具包是更方便的。 更多pyton下的操作可以参考: 用python...

Flask核心机制之上下文源码剖析

一、前言 了解过flask的python开发者想必都知道flask中核心机制莫过于上下文管理,当然学习flask如果不了解其中的处理流程,可能在很多问题上不能得到解决,当然我在写本篇文章...

Python with的用法

在Python中,with关键字是一个替你管理实现上下文协议对象的好东西。例如:file等。示例如下:    from __future__ import wi...

Python 使用 PyMysql、DBUtils 创建连接池提升性能

Python 使用 PyMysql、DBUtils 创建连接池提升性能

Python 编程中可以使用 PyMysql 进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接 MySQL 数据库请求时,都是独立的去请求访问,相当浪费资源,而且访问数量达到一...

Python多线程学习资料

一、Python中的线程使用: Python中使用线程有两种方式:函数或者用类来包装线程对象。 1、 函数式:调用thread模块中的start_new_thread()函数来产生新线程...