Python 是否更改像素的RGB值?

Python 是否更改像素的RGB值?,python,colors,rgb,pillow,Python,Colors,Rgb,Pillow,我正在尝试将我的图片上的像素更改为比我已经制作的更暗的绿色,我正在尝试向其添加+rgb(0,50,0),但我似乎无法这样做,您能帮助吗?我已经把我的代码放在下面,freljord2.png现在只是一个使用getcolor(绿色,“RGBA”)的全绿色图像 您提到希望使图像更暗绿色,但如果将图像的每个像素值增加50,则只会使图像更亮(更绿)。您要做的是在现有图像上放置一个绿色透明覆盖层。您应该为该图像创建一个与原始图像大小相同的新图像,并使用要添加的颜色和alpha值,该值指示图像的透明度。接下来

我正在尝试将我的图片上的像素更改为比我已经制作的更暗的绿色,我正在尝试向其添加+rgb(0,50,0),但我似乎无法这样做,您能帮助吗?我已经把我的代码放在下面,freljord2.png现在只是一个使用getcolor(绿色,“RGBA”)的全绿色图像


您提到希望使图像更暗绿色,但如果将图像的每个像素值增加50,则只会使图像更亮(更绿)。您要做的是在现有图像上放置一个绿色透明覆盖层。您应该为该图像创建一个与原始图像大小相同的新图像,并使用要添加的颜色和alpha值,该值指示图像的透明度。接下来,您需要使用遮罩将该图像粘贴到原始图像上

下面的代码示例应该可以做到这一点。结果如下所示。你可以利用这些价值观来适应你的需要

'''
Created on Oct 23, 2016

@author: physicalattraction
'''

import os.path
from PIL import Image


def get_img_dir() -> str:
    '''
    Return the full path to the image directory

    :return: string
    '''
    pkg_dir = os.path.dirname(__file__)
    img_dir = os.path.join(pkg_dir, '..', '..', 'img')
    return img_dir


def open_img(img_name: str) -> Image:
    '''
    Open the given file form the image directory

    :param img_name: Name including extension of the image
    :return: Image object
    '''
    img_dir = get_img_dir()
    full_img_path = os.path.join(img_dir, img_name)
    return Image.open(full_img_path)


def save_img(img: Image, img_name: str):
    '''
    Save the given image to the image directory

    :param img: Image object
    :param img_name: Name including the extension of the image
    '''
    img_dir = get_img_dir()
    full_img_path = os.path.join(img_dir, img_name)
    img.save(full_img_path)


def overlay(img: Image, overlay_color: tuple):
    '''
    Place an overlay over an existing image

    :param img: Image opened with PIL.Image
    :param overlay_color: four-tuple with color to add to your image
    '''
    assert len(overlay_color) == 4, 'Overlay color shall be a 4-tuple'

    img_overlay = Image.new(size=img.size, color=overlay_color, mode='RGBA')
    img.paste(img_overlay, None, mask=img_overlay)

    color_string = '_'.join([str(c) for c in overlay_color])
    filename = 'amsterdam_{color}.jpg'.format(color=color_string)
    save_img(img, filename)


if __name__ == '__main__':
    ams = open_img('amsterdam.jpg')
    green = (0, 50, 0, 128)
    overlay(ams, green)
原始图像:

深绿色图像:


您提到希望使图像更暗绿色,但如果将图像的每个像素值增加50,则只能使图像更亮(更绿)。您要做的是在现有图像上放置一个绿色透明覆盖层。您应该为该图像创建一个与原始图像大小相同的新图像,并使用要添加的颜色和alpha值,该值指示图像的透明度。接下来,您需要使用遮罩将该图像粘贴到原始图像上

下面的代码示例应该可以做到这一点。结果如下所示。你可以利用这些价值观来适应你的需要

'''
Created on Oct 23, 2016

@author: physicalattraction
'''

import os.path
from PIL import Image


def get_img_dir() -> str:
    '''
    Return the full path to the image directory

    :return: string
    '''
    pkg_dir = os.path.dirname(__file__)
    img_dir = os.path.join(pkg_dir, '..', '..', 'img')
    return img_dir


def open_img(img_name: str) -> Image:
    '''
    Open the given file form the image directory

    :param img_name: Name including extension of the image
    :return: Image object
    '''
    img_dir = get_img_dir()
    full_img_path = os.path.join(img_dir, img_name)
    return Image.open(full_img_path)


def save_img(img: Image, img_name: str):
    '''
    Save the given image to the image directory

    :param img: Image object
    :param img_name: Name including the extension of the image
    '''
    img_dir = get_img_dir()
    full_img_path = os.path.join(img_dir, img_name)
    img.save(full_img_path)


def overlay(img: Image, overlay_color: tuple):
    '''
    Place an overlay over an existing image

    :param img: Image opened with PIL.Image
    :param overlay_color: four-tuple with color to add to your image
    '''
    assert len(overlay_color) == 4, 'Overlay color shall be a 4-tuple'

    img_overlay = Image.new(size=img.size, color=overlay_color, mode='RGBA')
    img.paste(img_overlay, None, mask=img_overlay)

    color_string = '_'.join([str(c) for c in overlay_color])
    filename = 'amsterdam_{color}.jpg'.format(color=color_string)
    save_img(img, filename)


if __name__ == '__main__':
    ams = open_img('amsterdam.jpg')
    green = (0, 50, 0, 128)
    overlay(ams, green)
原始图像:

深绿色图像:


您能修复代码示例中的缩进吗?这在Python中非常重要。:-)您能在代码示例中修复缩进吗?这在Python中非常重要。:-)