打开图像并灰度化
# -*- coding: utf-8 -*-
from PIL import Image
from numpy import *
from pylab import *
import os
pil_im = Image.open('C:/pytm/ceshi.jpg').convert('L')
gray()
imshow(pil_im)
show()
对图片进行格式转换
# -*- coding: utf-8 -*-
from PCV.tools.imtools import get_imlist #导入原书的PCV模块
from PIL import Image
import os
import pickle
'''
对图片进行格式转换,转换后的图片保存在原图所在文件夹下
'''
filelist = get_imlist('C:/pytm/pic/')
imlist = file('C:/pytm/imlist.txt','w')
pickle.dump(filelist,imlist)#序列化
imlist.close()
for infile in filelist:
outfile = os.path.splitext(infile)[0]+".png"
if infile != outfile:
try:
Image.open(infile).save(outfile)
except IOError:
print "cannot convert",infile
把图像变成缩略图
# -*- coding: utf-8 -*-
from PIL import Image
from numpy import *
pil_im = Image.open('C:/pytm/pic/ceshi.jpg')
pil_im.thumbnail((125,125))
print pil_im.size
pil_im.save('C:/pytm/pic/slt.jpg') #保存缩略图
调整图片大小:
# -*- coding: utf-8 -*-
from PIL import Image
from numpy import *
pil_im = Image.open('C:/pytm/pic/ceshi.jpg')
pil_im = pil_im.resize((500,900))
print pil_im.size
pil_im.save('C:/pytm/pic/biaozhuntu1.jpg')
旋转图像45度
# -*- coding: utf-8 -*-
from PIL import Image
from numpy import *
pil_im = Image.open('C:/pytm/pic/test.jpg')
pil_im = pil_im.rotate(45)
print pil_im.size
pil_im.save('C:/pytm/pic/xuanzhuan1.jpg')