直接上code吧,暂记于此。
from PIL import Image #导入图像库中的Image
from pylab import *
pil_im_rgb = Image.open('e:\PC.jpg')
pil_im_rgb.show()
#(1)转化为灰度图像
pil_im_gray = Image.open('e:\PC.jpg').convert('L')
pil_im_gray.show()
#(2)转化为q其他格式的图片
pil_im_rgb.save('e:\PC.PNG');
pil_im_rgb.save('e:\PC.BMP');
#(3)图像缩放
pil_im_small = pil_im_gray.resize((240,240))
pil_im_small.show()
#(4)图像旋转,默认是逆时针旋转,如果为负数则变为顺时针旋转
pil_im_rotate = pil_im_small.rotate(45)
pil_im_rotate.show()
pil_im_rotate = pil_im_small.rotate(-45)
pil_im_rotate.show()
#(5)图像的宽高,指定位置的像素值
print pil_im_rgb.width
print pil_im_rgb.height
print pil_im_rgb.getpixel((1,1))
#查看帮助函数
help(Image)
兼容matlab的一些用法,其实是有一个叫做numpy的库。实现了类似matlab的功能。
from PIL import Image #导入图像库中的Image
from pylab import * #pylab 中包含matplotlib,为了兼容matlab的一些功能
# read image to array
im = array(Image.open('e:\PC.jpg').convert('L'))
# plot the image
imshow(im)
# some points
x = [100,100,400,400]
y = [200,500,200,500]
# plot the points with red star-markers
plot(x,y,'r*')
# line plot connecting the first two points
plot(x[:2],y[:2])
# add title and show the plot
title('testing')
show()
contour(im, origin='image')
axis('equal')
axis('off')