您有错误:
# 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
0
您有错误:
# Get the size of the image
width, height = picture.size()
for x in range(0, width - 1):
for y in range(0, height - 1):
我还建议您使用load(),因为它要快得多:
pix = im.load()
print pix[x, y]
pix[x, y] = value
0
我假设您正在尝试使用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)
0
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)
0
我想从我的Fluke机器人那里得到一张图像,并确定图像中每个像素的颜色。然后,如果像素大部分为红色,请将其更改为完全绿色。如果像素大部分为绿色,请将其更改为完全蓝色。如果像素大部分为蓝色,请将其更改为完全红色。这是我能够做的,但是我无法使它工作以获取必须更改的图像。没有语法错误,只是语义上的麻烦。我正在使用python。
我尝试的代码:
我也保存了如下图片: