【问题标题】:Change image color in PIL module在 PIL 模块中更改图像颜色
【发布时间】:2020-04-13 22:37:52
【问题描述】:

我正在尝试改变颜色的强度以获得不同的彩色图像...

import PIL
from PIL import Image
from PIL import ImageEnhance
from PIL import ImageDraw

# read image and convert to RGB
image=Image.open("readonly/msi_recruitment.gif")
image=image.convert('RGB')

# build a list of 9 images which have different brightnesses
enhancer=ImageEnhance.Brightness(image)
images=[]
for i in range(1, 10):
    images.append(enhancer.enhance(i/10))

# create a contact sheet from different brightnesses
first_image=images[0]
contact_sheet=PIL.Image.new(first_image.mode, (first_image.width*3,first_image.height*3))
x=0
y=0

for img in images:
    # Lets paste the current image into the contact sheet
    contact_sheet.paste(img, (x, y) )
    # Now we update our X position. If it is going to be the width of the image, then we set it to 0
    # and update Y as well to point to the next "line" of the contact sheet.
    if x+first_image.width == contact_sheet.width:
        x=0
        y=y+first_image.height
    else:
        x=x+first_image.width

# resize and display the contact sheet
contact_sheet = contact_sheet.resize((int(contact_sheet.width/2),int(contact_sheet.height/2) ))
display(contact_sheet)

但上面的代码只是改变亮度.... 请告诉我应该进行哪些更改以更改此代码中的颜色强度..... 很抱歉,我现在无法上传图片,请考虑您认为合适的任何图片并帮助我...感谢!!!!

请转到此链接并回答这个问题而不是这个问题,对于给您带来的不便,我深表歉意......

Pixel colour intensity

【问题讨论】:

    标签: python-3.x colors python-imaging-library


    【解决方案1】:

    许多颜色操作最好在 HSV 这样的颜色空间中完成,您可以在 PIL 中使用:

    HSV = rgb.convert('HSV')
    

    然后您可以使用split() 获得 3 个单独的频道:

    H, S, V = hsv.split()
    

    现在您可以更改颜色了。你对你想要的东西似乎有点毛骨悚然。如果你想改变颜色的强度,即降低饱和度和鲜艳度,请降低 S(饱和度)。如果您想将红色变为紫色,即更改色调,则在色调通道中添加一些内容。如果要使图像更亮或更暗,请更改 Value (V) 通道。

    完成后,将 merge((H,S,V)) 已编辑的通道重新合并在一起,并使用 convert('RGB') 转换回 RGB。

    请参阅this 页面上的拆分和合并处理单个频段


    这是一个例子,使用这张图片:

    这是加载图像、转换为 HSV 色彩空间、拆分通道、进行一些处理、重新组合通道并恢复为 RGB 色彩空间并保存结果的基本框架。

    #!/usr/bin/env python3
    
    from PIL import Image
    
    # Load image and create HSV version
    im = Image.open('colorwheel.jpg')
    HSV= im.convert('HSV')
    
    # Split into separate channels
    H, S, V = HSV.split()
    
    ######################################
    ########## PROCESSING HERE ###########
    ######################################
    
    # Recombine processed H, S and V back into a recombined image
    HSVr = Image.merge('HSV', (H,S,V))
    # Convert recombined HSV back to reconstituted RGB
    RGBr = HSVr.convert('RGB')
    
    # Save processed result
    RGBr.save('result.png')
    

    所以,如果你找到标记为 “PROCESSING HERE” 的块并在其中放入代码以将饱和度除以 2,它会使颜色不那么鲜艳:

    # Desaturate the colours by halving the saturation
    S = S.point(lambda p: p//2) 
    

    如果相反,我们将亮度 (V) 减半,如下所示:

    # Halve the brightness
    V=V.point(lambda p: p//2) 
    

    结果会变暗:

    如果相反,我们将色相加 80,所有颜色将围绕圆旋转 - 这称为 “色相旋转”

    # Rotate Hues around the Hue circle by 80 on a range of 0..255, so around 1/3 or a circle, i.e. 120 degrees:
    H = H.point(lambda p: p+80) 
    

    这给出了这个:

    【讨论】:

    • 这将如何应用于我的图像对象,因为我们没有在任何地方指定它......我是一个初学者,所以你能详细说明一下吗?
    • 非常感谢,我很感激,但是它仍然没有解决我的问题!!!! stackoverflow.com/questions/61188838/pixel-colour-intensity 转到此链接这是我实际上想问的问题。我很抱歉浪费您的时间,但请转到此链接并回答!谢谢
    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2013-03-29
    相关资源
    最近更新 更多