from PIL import Image
import numpy as np
a = np.array(Image.open("C:/Users/xpp/Desktop/Lena.png"))
print(a.shape, a.dtype)
b = [255, 255, 255]-a
im = Image.fromarray(b.astype('uint8'))
im.save('C:/Users/xpp/Desktop/Lena2.png')
注意:
b = [255, 255, 255]-a
im = Image.fromarray(b.astype('uint8'))
im.save('C:/Users/xpp/Desktop/Lena2.png')
修改为
b = 255 - a
im = Image.fromarray(b.astype('uint8'))
im.save('C:/Users/xpp/Desktop/Lena2.png')
注意:
from PIL import Image
import numpy as np
a = np.array(Image.open("C:/Users/xpp/Desktop/Lena.png"))
print(a.shape, a.dtype)
修改为
from PIL import Image
import numpy as np
a = np.array(Image.open("C:/Users/xpp/Desktop/Lena.png").convert('L'))
print(a.shape, a.dtype)
完整代码
from PIL import Image
import numpy as np
a = np.array(Image.open("C:/Users/xpp/Desktop/Lena.png").convert('L'))
print(a.shape, a.dtype)
b = 255 - a
im = Image.fromarray(b.astype('uint8'))
im.save('C:/Users/xpp/Desktop/Lena2.png')
from PIL import Image
import numpy as np
a = np.array(Image.open("C:/Users/xpp/Desktop/Lena.png").convert('L'))
c = (100/255)*a+150#区间变换
im = Image.fromarray(c.astype('uint8'))
im.save("C:/Users/xpp/Desktop/Lena2.png")
from PIL import Image
import numpy as np
a = np.array(Image.open("C:/Users/xpp/Desktop/Lena.png").convert('L'))
d = 255 * (a/255)**2#像素平方
im = Image.fromarray(c.astype('uint8'))
im.save("C:/Users/xpp/Desktop/Lena2.png")
扫码关注“图像处理与模式识别研究所”解锁更多技能哟。