红蓝互换:
# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
#读取图片并转为数组
im = array(Image.open("./source/test.jpg"))
#红色通道
r = im[:,:,0]
#交换红蓝通道并显示
im[:,:,0] = im[:,:,2]
im[:,:,2] = r
imshow(im)
show()
灰度转换:
# -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
#读取图片,灰度化,并转为数组
im = array(Image.open("./source/test.jpg").convert('L'))
im2 = 255 - im # 对图像进行反相处理
im3 = (100.0/255) * im + 100 # 将图像像素值变换到 100...200 区间
im4 = 255.0 * (im/255.0)**2 # 对图像像素值求平方后得到的图像(二次函数变换,使较暗的像素值变得更小)
#2x2显示结果 使用第一个显示原灰度图
subplot(221)
title('f(x) = x')
gray()
imshow(im)
#2x2显示结果 使用第二个显示反相图
subplot(222)
title('f(x) = 255 - x')
gray()
imshow(im2)
#2x2显示结果 使用第三个显示100-200图
subplot(223)
title('f(x) = (100/255)*x + 100')
gray()
imshow(im3)
#2x2显示结果 使用第四个显示二次函数变换图
subplot(224)
title('f(x) =255 *(x/255)^2')
gray()
imshow(im4)
show()
操作细节:将路径和文件名换为自己的,放在创建的.py文件目录内。



