#http://pillow.readthedocs.org/en/latest/index.html
#官方接口说明
# #coding:utf-8
# import os, sys
# from PIL import Image
# sys.argv.append("图片路径")#图片路径
'''Image 类'''
# im = Image.open(sys.argv[1])
# print(im.format,im.size,im.mode)#format定义格式,size表示宽X高,mode表示模式
# # L灰度,RGB真彩,CMYK pre-press
#
# size = (32,32)
# im.thumbnail(size)#转换大小
# im.save("/home/zyh/picture/the.jpg")#将打开的文件格式转换成jpg
#
'''读写图片'''
# #将图片转换成jpg格式
#
# for infile in sys.argv[1:]:#在终端运行时,加入的文件参数
# f,e = os.path.splitext(infile)#分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
# outfile = f +".jpg"
# # print e 输出图片格式
# if infile != outfile:
# try:
# Image.open(infile).save(outfile)
# except IOError:
# print("can't convert", infile)
# #创建缩略图
#
# size = (16,16)
# for infile in sys.argv[1:]:
# outfile = os.path.splitext(infile)[0]+".thumbnail"
# if infile != outfile:
# try:
# im = Image.open(infile)
# im.thumbnail(size)
# im.save(outfile,"JPEG")
# except IOError:
# print("can't creat thumbnail for",infile)
# #确定图片属性
# for infile in sys.argv[1:]:
# try :
# # with Image.open(infile) as im:#不知道为什么不对啊
# im = Image.open(infile)
# print(infile,im.format,"%d x %d"%im.size,im.mode)
# except IOError:
# pass
'''从图片中裁剪,粘贴,合并图片'''
# for infile in sys.argv[1:]:
# try :
# im = Image.open(infile)
# outfile = os.path.splitext(infile)[0]
#
# #从图片中复制子图
#
# # box = im.copy()#复制图片
# box = (im.size[0]/3,im.size[1]/3,im.size[0]*2/3,im.size[1]*2/3)#左上角为原点,left ,upper,right,lower
# region = im.crop(box)#从图片中提取子矩阵,
#
# #处理子图,粘贴回原图
#
# region = region.transpose(Image.ROTATE_180)#旋转180度
# im.paste(region,box)#将子图paste(粘贴)回原图,子图的region和原图吻合,
# im.save(outfile+"box.jpeg")
# except IOError:
# pass
# #rolling an Image
#
# def roll (image,delta):
# '''Roll an image sideways'''
# image = image.copy()
# xsize,ysize = image.size
# delta = delta%xsize
# if delta ==0 : return image
#
# part1 = image.crop((0,0,delta,ysize))
# part2 = image.crop((delta,0,xsize,ysize))
# image.paste(part2,(0,0,xsize-delta,ysize))
# image.paste(part1,(xsize-delta,0,xsize,ysize))
#
# return image
#
# for infile in sys.argv[1:]:
# try :
# im = Image.open(infile)
# outfile = os.path.splitext(infile)[0]
#
# #从图片中复制子图
# roll(im.copy(),300).save(outfile+"roll.jpeg")
# #im.save(outfile+"box.jpeg")
# except IOError:
# pass
# #分离合并通道
# for infile in sys.argv[1:]:
# try :
# im = Image.open(infile)
# outfile = os.path.splitext(infile)[0]
# r,g,b= im.split()#对於单通道图片,split()返回图像本身
# im = Image.merge("RGB",(b,r,g))#转换(RGB)的位置
# im.save(outfile+"tongdao.jpeg")
# #im.save(outfile+"box.jpeg")
# except IOError:
# pass
#
''' 几何变换'''
# for infile in sys.argv[1:]:
# try :
# im = Image.open(infile)
# outfile = os.path.splitext(infile)[0]
# #简单的几何变换
#
# out = im.resize((64,64))# 改变大小
# out = im.rotate(30) # 顺时针角度表示
# out.save(outfile+"jihe简单.jpeg")
# #置换图像
# out = im.transpose(Image.FLIP_LEFT_RIGHT) #左右调换
# out.save(outfile+"jiheLR.jpeg")
# out = im.transpose(Image.FLIP_TOP_BOTTOM) #上下调换
# out.save(outfile+"jiheTB.jpeg")
# out = im.transpose(Image.ROTATE_90) #右旋90,可以用rotate替换
# out.save(outfile+"jiheR45.jpeg")
# out = im.transpose(Image.ROTATE_180)
# out.save(outfile+"jiheR180.jpeg")
# out = im.transpose(Image.ROTATE_270)
#
# out.save(outfile+"jiheR270.jpeg")
# #im.save(outfile+"box.jpeg")
# except IOError:
# pass
'''模式转换'''
#
# for infile in sys.argv[1:]:
# try :
# im = Image.open(infile)
# outfile = os.path.splitext(infile)[0]
# #简单的几何变换
# out = im.convert("L")
# out.save(outfile+"convertL.jpeg")
# except IOError:
# pass
'''图像增强()'''
# for infile in sys.argv[1:]:
# try :
# im = Image.open(infile)
# outfile = os.path.splitext(infile)[0]
# #简单的几何变换
#
# from PIL import ImageFilter
# out = im.filter(ImageFilter.DETAIL)
# out.save(outfile+"filter.jpeg")
# except IOError:
# pass
'''像素点处理'''
# for infile in sys.argv[1:]:
# try :
# im = Image.open(infile)
# outfile = os.path.splitext(infile)[0]
#
# #像素点变换
#
# out = im.point(lambda i: i * 0.3)#每个像素乘0.3,通过point和paste选择性的处理图片的某一区域
# out.save(outfile+"point.jpeg")
#
# #处理单独通道
#
# source = im.split()
# R,G,B = 0,1,2
#
# # select regions where red is less than 100
# mask = source[R].point(lambda i: i<100 and 255)
# #mask参数用法:当为0时,保留当前值,255为使用paste进来的值,中间则用于transparency效果
#
# # process the green band
# out = source[G].point(lambda i: i*10)
#
# # paste the processed band back, but only where red was < 100
# source[G].paste(out,None,mask)
#
# im=Image.merge(im.mode,source)
# im.save(outfile+"tongdao.jpeg")
# except IOError:
# pass
python图像处理接口
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.