我想从我的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
我假设您正在尝试使用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)
你有错误:
# Get the size of the image
width, height = picture.size()
for x in range(0, width - 1):
for y in range(0, height - 1):
- 括号是错误的!省略它们。
- int是不可思的。
我也建议您使用load(),因为它快得多:
pix = im.load()
print pix[x, y]
pix[x, y] = value
我使用的是python 3.6.4和枕头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)
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)
在Gizmo的答案上添加一些评论。这个:
px = im.load()
current_color = (px[i, j])
可能比以下速度快:
picture.getpixel( (x,y) )
另外,请确保使用此信息:
picture.putdata(colors)
而不是在循环中:
picture.putpixel( (x,y), new_color)
在图片上更改Pixel的颜色。
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')