15

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
Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
Q.matin
  • 287
  • 1
  • 2
  • 7

6 Answers6

23

I assume you're trying to use the Image module. 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)
Gizmo
  • 727
  • 6
  • 7
  • 1
    Thank you a lot! So if i needed to change the pixel color into a specific color (mentioed in my question) I will have to use your code but put in a for loop and then "if" one is a color i will return it to the color I want? – Q.matin Oct 31 '12 at 21:11
  • Updated to handle your specific use a little more clearly. You can use `R,G,B = current_color` in order to separate the individual colors out and use `new_color = (R,G,B)` to convert back again. – Gizmo Oct 31 '12 at 21:18
  • This version is not working with python 3.6.4 and Pillow 5.0.0. I putting the updated version bellow, feel free to integrate in your answer. – leonprou Jan 16 '18 at 10:12
8

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.

I also recommend you to use load(), because it's much faster :

pix = im.load()

print pix[x, y]

pix[x, y] = value
maorm1
  • 81
  • 1
  • 2
3

I'm using python 3.6.4 and Pillow 5.0.0. Gizmo's code snippet didn't work for me. 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)
leonprou
  • 4,638
  • 3
  • 21
  • 27
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)
Goyal Vicky
  • 1,249
  • 16
  • 16
2

Adding some remarks on Gizmo's answer. 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)
saldukoo
  • 161
  • 5
1

Change color of pixel on picture Меняем цвет пикселя в картинке(меняем фон)

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')
Tranbi
  • 11,407
  • 6
  • 16
  • 33
Alex
  • 29
  • 2