根据目前的回应,我猜答案是否定的。
但你可以使用numpy来高效地完成这种工作:
def colorswap(pixel):
"""Shifts the channels of the image."""
return (pixel[1], pixel[2], pixel[0])
def npoint(img, func):
import numpy as np
a = np.asarray(img).copy()
r = a[:,:,0]
g = a[:,:,1]
b = a[:,:,2]
r[:],g[:],b[:] = func((r,g,b))
return Image.fromarray(a,img.mode)
img = Image.open('test.png')
img2 = npoint(img, colorswap)
img2.save('test2.png')
奖励:看起来Image类不是只读的,这意味着你可以让你的新的npoint函数更像point(除非你喜欢让人迷惑,否则不建议这样做):
Image.Image.npoint = npoint
img = Image.open('test.png')
img2 = img.npoint(colorswap)
image.load()
方法正是我所需要的。谢谢! - Haldean Brown