更改像素颜色 Python

16

我需要从我的Fluke机器人中获取一张图片,并确定图像中每个像素的颜色。然后,如果该像素大部分是红色,则将其更改为完全绿色。如果像素大部分是绿色,则将其更改为完全蓝色。如果像素大部分是蓝色,则将其更改为完全红色。这是我能做到的,但我无法让它工作以获取我要更改的图像。没有语法错误,只是语义上有问题。我正在使用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

你尝试了什么?你在哪里需要帮助? - thegrinner
6个回答

24

我假设你正在尝试使用Image模块。以下是一个示例:

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)

1
非常感谢!所以,如果我需要将像素颜色更改为特定的颜色(在我的问题中提到),我将不得不使用您的代码,但放入一个for循环,然后“if”其中一个是颜色,我将返回我想要的颜色? - Q.matin
更新以更清晰地处理您的特定用途。您可以使用“R,G,B = current_color”来分离各个颜色,并使用“new_color =(R,G,B)”进行转换。 - Gizmo
这个版本不支持Python 3.6.4和Pillow 5.0.0。我在下面放置了更新的版本,请随意将其集成到您的答案中。 - leonprou

8

你犯了错误:

# 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

3

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

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

2

对Gizmo的回答进行一些补充。以下是:

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

这可能比这个更快:

picture.getpixel( (x,y) )

另外,请确保使用以下内容:

 picture.putdata(colors)

循环中不要使用以下代码:

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

1

修改图片上像素的颜色,改变像素的背景。

https://colorscheme.ru/color-converter.html¶

在图片中找到一种颜色并将其更改为其他颜色。

import numpy as np  
from PIL  
import Image 
import os,sys

im = Image.open('1.png')  
data = np.array(im)

#Original value(цвет который будем менять)  
r1, g1, b1 = 90, 227, 129 

#Value that we want to replace it with(цвет выхода)  
r2, g2, b2 = 140, 255, 251

red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2] 
 
mask = (red == r1) & (green == g1) & (blue == b1)
  
data[:,:,:3][mask] = [r2, g2, b2]

im = Image.fromarray(data) 
 
im.save('1_mod.png')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接