简体   繁体   English

更改像素颜色 Python

[英]Changing pixel color Python

I am suppose to get an image from my fluke robot and determine the color of each pixel in my image.我想从我的侥幸机器人那里得到一张图像,并确定我的图像中每个像素的颜色。 Then if the pixel is mostly red, change it to completely green.然后,如果像素大部分为红色,请将其更改为完全绿色。 If the pixel is mostly green, change it to completely blue.如果像素大部分为绿色,请将其更改为完全蓝色。 If the pixel is mostly blue, change it to completely red.如果像素大部分为蓝色,请将其更改为完全红色。 This is what I am able to do, but I can't get it to work to get the image I have to change.这是我能够做的,但我无法让它工作以获取我必须更改的图像。 There is no syntax error, it is just semantic I am having trouble with.没有语法错误,这只是我遇到的语义问题。 I am using python.我正在使用蟒蛇。

My attempted code:我尝试的代码:

import getpixel
getpixel.enable(im)  
r, g, b = im.getpixel(0,0)  
print 'Red: %s, Green:%s, Blue:%s' % (r,g,b)

Also I have the picture saved like the following:我还保存了如下图片:

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

I assume you're trying to use the Image module. 我假设您正在尝试使用Image模块。 Here's an example: 这是一个例子:

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

Running this on this image I get the output: 在此图像上运行此命令,我得到输出:

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

EDIT: To do what you want I would try something like this 编辑:要做你想做的事我会尝试这样的事情

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)

You have mistakes: 你有错误:

# 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. Brackets are mistake!! 括号是错误的!! omit them. 省略它们。
  2. int is not iterable. int不可迭代。

I also recommend you to use load(), because it's much faster : 我还建议你使用load(),因为它更快:

pix = im.load()

print pix[x, y]

pix[x, y] = value
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)

I'm using python 3.6.4 and Pillow 5.0.0. 我正在使用python 3.6.4和Pillow 5.0.0。 Gizmo's code snippet didn't work for me. Gizmo的代码片段对我不起作用。 After little work I created a fixed snippet: 经过一番努力,我创建了一个固定的片段:

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)

Adding some remarks on Gizmo's answer. 在Gizmo的答案上添加一些评论。 This: 这个:

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

is probably faster than this: 可能比这更快:

picture.getpixel( (x,y) )

Also, make sure to use this: 另外,请务必使用此:

 picture.putdata(colors)

instead of this inside the loop: 而不是在循环内:

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

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

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

Find one color on picture and change На всей картинке находит цвет и заменяет его другим.在图片上找到一种颜色并更改 На всей картинке находит цвет и заменяет его другим。

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

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

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