-
-
Save iamlixiao/9490eed1929f340ffd32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# -*- coding: utf-8 -*- | |
from PIL import Image | |
from colorsys import rgb_to_hls, hls_to_rgb | |
#由于抗锯齿及缩放算法的应用,色块的交界处通常会存在变亮和变暗了的像素 | |
#以红色和黑色的交界线为例,放大后可发现这条线上像素的颜色由红色和黑色混合而成 | |
#对于颜色和黑/白色的混合,一个简单的处理方法可以应用于HLS空间的颜色 | |
def hls(r, g, b): | |
return rgb_to_hls(r/255.0, g/255.0, b/255.0) | |
#色相差距的计算并不是普通的求绝对值,因为值为0和1的色相是相同的 | |
def hueDiffAbs(h1, h2): | |
absh = abs(h1 - h2) | |
return absh if absh < 0.5 else 1 - absh | |
src_color = (231, 20, 26) | |
src_color_hls = hls(*src_color) | |
target_color = (0, 0, 0xFF) | |
target_color_hls = hls(*target_color) | |
im = Image.open('9_019.png') | |
canvas = im.copy() | |
w, h = im.size | |
for x in range(w): | |
for y in range(h): | |
r, g, b, a = im.getpixel((x, y)) | |
ch, cl, cs = hls(r, g, b) | |
#当前像素和源色的比值反映了源色和黑/白色的混合情况 | |
lightnessRatio = cl / src_color_hls[1] | |
#色相和饱和度的差距反映了颜色的差异 | |
d = hueDiffAbs(ch, src_color_hls[0]) + 0.5 * abs(cs - src_color_hls[2]) | |
if d < 0.13: | |
#最终像素的颜色,色相和饱和度都照搬目标颜色,但亮度要根据源图作调整 | |
r, g, b = [int(v*255) for v in hls_to_rgb(target_color_hls[0], target_color_hls[1] * lightnessRatio, target_color_hls[2])] | |
canvas.putpixel((x, y), (r, g, b, a)) | |
canvas.save('9_019_gm.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment