python圖像處理接口

#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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章