【发布时间】:2017-07-21 06:36:34
【问题描述】:
我正在尝试将 RGB 图片转换为灰度。我不想使用 image.convert('L')。这只是显示原始图像而不更改任何内容。我尝试在“red,green,blue=0,0,0”行中添加不同的数字,这确实会改变图像的颜色,但这不是我想要的。
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'))
【问题讨论】: