【问题标题】:Changing pixel color Python更改像素颜色 Python
【发布时间】:2012-10-31 20:54:23
【问题描述】:

我想从我的 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

【问题讨论】:

标签: python image-processing


【解决方案1】:

我假设您正在尝试使用 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))

在这个image 上运行它,我得到了输出:

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

【讨论】:

  • 非常感谢!因此,如果我需要将像素颜色更改为特定颜色(在我的问题中提到),我将不得不使用您的代码,但放入一个 for 循环,然后“如果”一个是一种颜色,我会将其返回为我想要的颜色?
  • 已更新以更清楚地处理您的特定用途。您可以使用R,G,B = current_color 将各个颜色分开,然后使用new_color = (R,G,B) 再次转换回来。
  • 此版本不适用于 python 3.6.4 和 Pillow 5.0.0。我将更新版本放在下面,请随时集成到您的答案中。
【解决方案2】:

你有错误:

# 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 的代码 sn-p 对我不起作用。经过一些工作,我创建了一个固定的 sn-p:

    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)
    

    【讨论】:

      【解决方案4】:
      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)
      

      【讨论】:

        【解决方案5】:

        对 Gizmo 的回答添加一些评论。这个:

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

        可能比这更快:

        picture.getpixel( (x,y) )
        

        另外,一定要使用这个:

         picture.putdata(colors)
        

        而不是循环内的这个:

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

        【讨论】:

          【解决方案6】:

          更改图片上像素的颜色 Меняем цвет пикселя в картинке(меняем фон)

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

          【讨论】:

            猜你喜欢
            • 2019-01-24
            • 1970-01-01
            • 1970-01-01
            • 2019-08-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多