0
我试图将RGB图片转换为灰度。我不想使用image.convert('L')。这只是显示原始图像而不更改任何内容。我曾尝试在“红色,绿色,蓝色= 0,0,0”行中放入不同的数字,这会改变图像的颜色,但这不是我想要的。在Python中使用PIL将像素更改为灰度
import PIL
from PIL import Image
def grayscale(picture):
res=PIL.Image.new(picture.mode, picture.size)
width, height = picture.size
for i in range(0, width):
for j in range(0, height):
red, green, blue = 0,0,0
pixel=picture.getpixel((i,j))
red=red+pixel[0]
green=green+pixel[1]
blue=blue+pixel[2]
avg=(pixel[0]+pixel[1]+pixel[2])/3
res.putpixel((i,j),(red,green,blue))
res.show()
grayscale(Image.show('flower.jpg'))