本文介绍了使用python改变颜色的色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来改变颜色的色调,知道它的RGB组成,然后用获得的RGB替换旧的RGB的所有实例。例如,我想要红色变成紫色,浅红色,浅紫色等等。
可以通过改变颜色的色调在photoshop中完成。



到目前为止,我想的是以下:将RGB转换为HLS,然后更改Hue。



这里的代码到目前为止不只是一个,在列表列表中定义):



(你可能会注意到,我只是一个初学者,代码本身很脏;部分可能取自其他SO用户)
非常感谢!

  import colorsys 

从tempfile import mkstemp
从shutil import move
从os import remove,关闭

def replace(file,pattern,subst):
#Create temp file
fh,abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(file)
在old_file中的行:
new_file.write (line.replace(pattern,subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove原始文件
remove(file)
#移动新文件
move(abs_path,file)

def decimal(var):
return' g}'。format(float(var))

list = [[60,60,60],[15,104,150],[143,185,215],[231,231,231],[27,161,253] ,43],[56,56,56],[255,255,255],[45,45,45],[5,8,10],[23,124,193],[47,81,105],[125,125,125],[ ,0],[24,24,24],[0,109,166],[0,170,255],[127,127,127]]

在范围list [i] [0] / 255
g = list [i] [1] / 255
b = list [i] [2] / 255
h,l,s = colorsys.rgb_to_hls r,g,b)
print(decimal(r * 255),decimal(g * 255),decimal(b * 255))
h = 300/360
str1 = str (r * 255))+,+ str(decimal(g * 255))+,+ str(decimal(b * 255))
r,g,b = colorsys.hls_to_rgb(h,l ,s)
print(decimal(r * 255),decimal(g * 255),decimal(b * 255))
str2 = str(decimal(r * 255))+ str(decimal(g * 255))+,+ str(decimal(b * 255))
replace(Themes.xml,str1,str2)



编辑:问题很简单:R,G,B和H必须在0和1之间,我设置他们在0和255和0

解决方案

您的颜色序列使用整数,但 colorsys 使用0.0和1.0之间的浮点值。将所有数字除以 255。,然后再乘以255,并在返回后截断。


I'm looking for a way to change a color's tone, knowing it's RGB composition, then replace all instances of the old RGB with the obtained RGB. For example, I'd like red to become purple, light red, light purple, etc... It can be done in photoshop by changing a color's hue.

What I've thought so far is the following: convert RGB to HLS, then change the Hue.

Here's the code so far (multiple colors are changed, not just one, defined in the "list" list):

(As you might notice, I'm just a beginner and the code itself is pretty dirty; the cleaner parts are probably taken from other SO users) Thanks a lot!

import colorsys

from tempfile import mkstemp
from shutil import move
from os import remove, close

def replace(file, pattern, subst):
    #Create temp file
    fh, abs_path = mkstemp()
    new_file = open(abs_path,'w')
    old_file = open(file)
    for line in old_file:
        new_file.write(line.replace(pattern, subst))
    #close temp file
    new_file.close()
    close(fh)
    old_file.close()
    #Remove original file
    remove(file)
    #Move new file
    move(abs_path, file)

def decimal(var):
    return '{:g}'.format(float(var))

list=[[60,60,60],[15,104,150],[143,185,215],[231,231,231],[27,161,253],[43,43,43],[56,56,56],[255,255,255],[45,45,45],[5,8,10],[23,124,193],[47,81,105],[125,125,125],[0,0,0],[24,24,24],[0,109,166],[0,170,255],[127,127,127]]

for i in range(0,len(list)):
    r=list[i][0]/255
    g=list[i][1]/255
    b=list[i][2]/255
    h,l,s=colorsys.rgb_to_hls(r,g,b)
    print(decimal(r*255),decimal(g*255),decimal(b*255))
    h=300/360
    str1=str(decimal(r*255)) + "," + str(decimal(g*255)) + "," + str(decimal(b*255))
    r,g,b=colorsys.hls_to_rgb(h, l, s)
    print(decimal(r*255),decimal(g*255),decimal(b*255))
    str2=str(decimal(r*255)) + "," + str(decimal(g*255)) + "," + str(decimal(b*255))
    replace("Themes.xml",str1,str2)

EDIT: The problem was pretty simple: R,G,B and H must be between 0 and 1, I was setting them between 0 and 255 and 0 and 360. Updated code.

解决方案

Your sequence of colors uses integers, but colorsys uses floating point values between 0.0 and 1.0. Divide all the numbers by 255. before putting them through, then multiply by 255 and truncate after getting them back.

这篇关于使用python改变颜色的色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:38