在做图像处理的时候,我们需要使用很多图像库以及工具库,PIL(Python Imaging Library,图像处理类库),提供了通用的图像处理功能(图像缩放、裁剪、旋转、颜色转换等),这些都是图像在处理之前的准备工作。
from PIL import Image
pil_im = Image.open("lena.jpg")
pil_im.show()
我们使用可lena图像,就会出现lena图像:
如果我们需要显示灰度图像:
转换图像格式
for infile in filelist:
outfile = os.path.splitext(infile)[0]+".jpg"
if infile != outfile:
try:
Image.open(infile).save(outfile)
except IOError:
print "can not convert "+infile
我们在大量处理图片,需要形成图片列表,但是有些图片不太合适,需要更换图片格式,请注意,这个所做的变换不会更改图片的内容,只是更改了图片的打开方式。
创建缩略图
pil_im.thumbnail((128,128))
pil_im.show()
复制和粘贴
裁剪指定区域:
box = (100,100,400,400) (左,上,右