2012-10-31 166 views
7

我想从我的侥幸机器人获取图像并确定图像中每个像素的颜色。然后,如果像素大多是红色,请将其更改为完全绿色。如果像素大部分是绿色,则将其更改为完全蓝色。如果像素大部分是蓝色,则将其更改为完全红色。这是我能够做到的,但我无法让它工作来获得我必须改变的图像。没有语法错误,它只是语义错误。我正在使用python。更改像素颜色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 
+0

(http://whathaveyoutried.com)你在哪里需要帮助吗? – thegrinner

+0

我已编辑我的上述帖子 –

回答

11

我假设你想使用Image模块。这里有一个例子:

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

这个image运行此我得到的输出:

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

编辑: 做你想做我想尝试这样的

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) 
+0

非常感谢!因此,如果我需要将像素颜色更改为特定颜色(在我的问题中采用mentioed),我将不得不使用您的代码,但要放入for循环,然后“如果”一个颜色,我会将其返回到我想要的颜色? –

+0

已更新,以便更清楚地处理您的具体使用。您可以使用'R,G,B = current_color'来分离各个颜色,并使用'new_color =(R,G,B)'来再次转换。 – Gizmo

+0

非常感谢你Gizmo! –

3

你的东西是什么有错误:

# Get the size of the image 

width, height = picture.size() 

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

     for y in range(0, height - 1): 
  1. 括号是错误的!省略它们。
  2. int不可迭代。

我也建议你使用load(),因为它的速度更快:

pix = im.load() 

print pix[x, y] 

pix[x, y] = value 
0

添加上小物件的回答一些言论。这:

px = im.load() 
current_color = (px[i, j]) 

可能比这个速度更快:

picture.getpixel((x,y)) 

此外,请务必使用此:

picture.putdata(colors) 

,而不是这个循环中:

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

我使用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) 
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)