Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 改变像素颜色_Python_Image Processing - Fatal编程技术网

Python 改变像素颜色

Python 改变像素颜色,python,image-processing,Python,Image Processing,我想从我的福禄克机器人那里得到一张图像,并确定图像中每个像素的颜色。然后,如果像素大部分为红色,则将其更改为完全绿色。如果像素大部分为绿色,则将其更改为完全蓝色。如果像素大部分为蓝色,则将其更改为完全红色。这是我能做的,但我无法让它工作,以获得我必须改变的形象。没有语法错误,只是语义问题。我正在使用python 我的尝试代码: import getpixel getpixel.enable(im) r, g, b = im.getpixel(0,0) print 'Red: %s, Gr

我想从我的福禄克机器人那里得到一张图像,并确定图像中每个像素的颜色。然后,如果像素大部分为红色,则将其更改为完全绿色。如果像素大部分为绿色,则将其更改为完全蓝色。如果像素大部分为蓝色,则将其更改为完全红色。这是我能做的,但我无法让它工作,以获得我必须改变的形象。没有语法错误,只是语义问题。我正在使用python

我的尝试代码:

import getpixel
getpixel.enable(im)  
r, g, b = im.getpixel(0,0)  
print 'Red: %s, Green:%s, Blue:%s' % (r,g,b)
我还保存了图片,如下所示:

pic1 = makePicture("pic1.jpg"):
    for pixel in getpixel("pic1.jpg"):
        if pixel Red: %s:
           return Green:%s
        if pixel Green:%s: 
           return Blue:%s

我假设您正在尝试使用
图像
模块。下面是一个例子:

from PIL import Image
picture = Image.open("/path/to/my/picture.jpg")
r,g,b = picture.getpixel( (0,0) )
print("Red: {0}, Green: {1}, Blue: {2}".format(r,g,b))
在此基础上运行此命令,我将获得以下输出:

>>> from PIL import Image
>>> picture = Image.open("/home/gizmo/Downloads/image_launch_a5.jpg")
>>> r,g,b = picture.getpixel( (0,0) )
>>> print("Red: {0}, Green: {1}, Blue: {2}".format(r,g,b))
Red: 138, Green: 161, Blue: 175
编辑: 要做你想做的事,我会试试这样的

from PIL import Image
picture = Image.open("/path/to/my/picture.jpg")

# Get the size of the image
width, height = picture.size()

# Process every pixel
for x in width:
   for y in height:
       current_color = picture.getpixel( (x,y) )
       ####################################################################
       # Do your logic here and create a new (R,G,B) tuple called new_color
       ####################################################################
       picture.putpixel( (x,y), new_color)
你有错误:

# Get the size of the image

width, height = picture.size()

for x in range(0, width - 1):

        for y in range(0, height - 1):
  • 括号是错误的!!省略它们
  • int是不可数的
  • 我还建议您使用load(),因为它要快得多:

    pix = im.load()
    
    print pix[x, y]
    
    pix[x, y] = value
    

    对Gizmo的答案添加一些注释。这:

    px = im.load()
    current_color = (px[i, j])
    
    可能比这更快:

    picture.getpixel( (x,y) )
    
    此外,请确保使用以下选项:

     picture.putdata(colors)
    
    而不是在循环内部执行此操作:

    picture.putpixel( (x,y), new_color)
    

    我正在使用python 3.6.4和Pillow 5.0.0。Gizmo的代码片段对我不起作用。在做了一些工作之后,我创建了一个固定的代码段:

    import Image
    picture = Image.open("/path/to/my/picture.jpg")
    
    # Get the size of the image
    width, height = picture.size
    
    # Process every pixel
    for x in range(width):
       for y in range(height):
           current_color = picture.getpixel( (x,y) )
           ####################################################################
           # Do your logic here and create a new (R,G,B) tuple called new_color
           ####################################################################
           picture.putpixel( (x,y), new_color)
    

    你在哪里需要帮助?非常感谢!因此,如果我需要将像素颜色更改为特定颜色(我的问题中提到),我将不得不使用您的代码,但将其放入for循环,然后“如果”一个是颜色,我将返回我想要的颜色?更新以更清楚地处理您的特定用途。您可以使用
    R,G,B=current\u color
    将各个颜色分离出来,然后使用
    new\u color=(R,G,B)
    再次转换回来。此版本不适用于python 3.6.4和Pizzle 5.0.0。我把更新的版本放在下面,请随意整合到您的答案中。
    import cv2
    import numpy as np
    
    m =  cv2.imread("C:/../Sample Pictures/yourImage.jpg")
    
    h,w,bpp = np.shape(m)
    
    for py in range(0,h):
        for px in range(0,w):
    #can change the below logic of rgb according to requirements. In this 
    #white background is changed to #e8e8e8  corresponding to 232,232,232 
    #intensity, red color of the image is retained.
            if(m[py][px][0] >200):            
                m[py][px][0]=232
                m[py][px][1]=232
                m[py][px][2]=232
    
    cv2.imshow('matrix', m)
    cv2.waitKey(0)
    cv2.imwrite('yourNewImage.jpg',m)