简体   繁体   English

Python枕头改变像素的RGB值?

[英]Python Pillow Changing RGB Value Of Pixels?

I'm trying to change the pixels on my picture to darker green than I have already made it, I am trying to add + rgb(0,50,0) to it but I can't seem to be able to do so, could you help? 我正试图将我的照片上的像素更改为比我已经制作的更深的绿色,我正在尝试添加+ rgb(0,50,0),但我似乎无法这样做,你能帮忙吗? I have put my code in below, and freljord2.png is now just a full green image using getcolor(green, "RGBA") 我把我的代码放在下面,freljord2.png现在只是一个使用getcolor的全绿色图像(绿色,“RGBA”)

im = Image.open('freljord2.png')
#one_pixel = im.getpixel((0, 0))
#one_pixel[1] = 0;
#im.save('freljord3.png')


(0, 0, 0, 0)
for x in range(0):
for y in range(0):
    im.putpixel((x, y), (210, 210, 210))
for x in range(560):
    for y in range(557):
    print("hi")
    hello = ImageColor.get(00B200)
    im.putpixel((x, y), )
im.getpixel((0, 0))
(210, 210, 210, 255)
im.getpixel((0, 50))
(169, 169, 169, 255)
im.save('freljord2.png')

You mention that you want to make your image more dark green, but if you add 50 to each pixel value of an image, you only make it brighter (and greener). 你提到你想让你的图像更加深绿色,但如果你为图像的每个像素值加50,你只会使它更亮(和更绿)。 What you want to do is to place a green transparent overlay over your existing image. 您要做的是在现有图像上放置绿色透明覆盖图。 You should create a new image for that with the same size as your original with the color you want to add, and an alpha value, which indicates how transparent it is. 您应该为原始图像创建一个新图像,其大小与您想要添加的颜色相同,以及一个alpha值,表示它的透明度。 Next, you need to paste that image over your original image using a mask. 接下来,您需要使用蒙版将该图像粘贴到原始图像上。

The following code example should do the trick. 下面的代码示例应该可以解决问题。 The result is shown below. 结果如下所示。 You can play around a little bit with the values to tailor to your needs. 你可以玩一下这些价值观来定制你的需求。

'''
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)

Original image: 原始图片:

原阿姆斯特丹图像

Darker green image: 深绿色图像:

较暗的绿色图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM