使用更好的名称,跳过加载/存储/重新加载图像。在
您正在处理错误的图像数据-您从x = pixelsNew[j,i][0]读取像素,这是您新创建的图像-它还没有tigerdata。在
我更喜欢使用RGB,所以我可以调整灰度以使用R over B等。如果你想操作“LA”图像,取消注释“LA”行并注释“RGB”行。在from PIL import Image
def grayscale(picture, thresh):
"""Grayscale converts picture, assures resulting pictures are
inside range thres by limiting lowest/highest values inside range"""
res = Image.new(picture.mode, picture.size)
width, height = picture.size
minT = min(thresh)
maxT = max(thresh)
for i in range(0, width):
for j in range(0, height):
pixel = picture.getpixel((i,j))
a = int( (pixel[0] + pixel[1] + pixel[2]) / 3) # RGB
# a = pixel[0] # LA
a = a if a in thresh else (minT if a < minT else maxT)
res.putpixel((i,j),(a,a,a)) # RGB
# res.putpixe