python使用pil進(jìn)行圖像處理(等比例壓縮、裁剪)實(shí)例代碼
PIL中設(shè)計(jì)的幾個(gè)基本概念
1.通道(bands):即使圖像的波段數(shù),RGB圖像,灰度圖像
以RGB圖像為例:
>>>from PIL import Image >>>im = Image.open('*.jpg') # 打開(kāi)一張RGB圖像 >>>im_bands = im.g etbands() # 獲取RGB三個(gè)波段 >>>len(im_bands) >>>print im_bands[0,1,2] # 輸出RGB三個(gè)值
2.模式(mode):定義了圖像的類型和像素的位寬。共計(jì)9種模式:
>>> im.mode
① 1:1位像素,表示黑和白,但是存儲(chǔ)的時(shí)候每個(gè)像素存儲(chǔ)為8bit。 ② L:8位像素,表示黑和白。 ③ P:8位像素,使用調(diào)色板映射到其他模式。 ④ RGB:3x8位像素,為真彩色。 ⑤ RGBA:4x8位像素,有透明通道的真彩色。 ⑥ CMYK:4x8位像素,顏色分離。 ⑦ YCbCr:3x8位像素,彩色視頻格式。 ⑧ I:32位整型像素。 ⑨ F:32位浮點(diǎn)型像素。
3.尺寸(size):獲取圖像水平和垂直方向上的像素?cái)?shù)
>>> im.size()
4.坐標(biāo)系統(tǒng)(coordinate system):
PIL使用笛卡爾像素坐標(biāo)系統(tǒng),坐標(biāo)(0,0)位于左上角。
注意:坐標(biāo)值表示像素的角;位于坐標(biāo)(0,0)處的像素的中心實(shí)際上位于(0.5,0.5)。
5.調(diào)色板(palette):
調(diào)色板模式("P")適用一個(gè)顏色調(diào)色板為每一個(gè)像素定義具體的顏色值。
6.信息(info)
>>> im.info() # 返回值為字典對(duì)象
7.濾波器(filters):將多個(gè)輸入像素映射為一個(gè)輸出像素的幾何操作
PIL提供了4種不同的采樣濾波器:
① NEAREST:最近濾波。從輸入圖像中選取最近的像素作為輸出像素。
② BILINEAR:雙線性內(nèi)插濾波。在輸入圖像的2*2矩陣上進(jìn)行線性插值。
③ BICUBIC:雙立方濾波。在輸入圖像的4*4矩陣上進(jìn)行立方插值。
④ ANTIALIAS:平滑濾波。對(duì)所有可以影響輸出像素的輸入像素進(jìn)行高質(zhì)量的重采樣濾波,以計(jì)算輸出像素值。
im.resize()和im.thumbnail()用到了濾波器
方法一:resize(size,filter = None)
>>> from PIL import Image >>> im = Image.open('*.jpg') >>> im.size >>> im_resize = im.resize((256,256)) #default 情況下是NEAREST插值方法 >>> im_resize0 = im.resize((256,256), Image.BILINEAR) >>> im_resize0.size >>> im_resize1 = im.resize((256,256), Image.BICUBIC) >>> im_resize2 = im.resize((256,256), Image.ANTIALIAS)
方法二:im.thumbnail(size,filter = None)
對(duì)于pil的相關(guān)介紹就到這里了,下面分享一個(gè)使用pil進(jìn)行圖像處理(等比例壓縮、裁剪)實(shí)例代碼,如下:
#coding:utf-8 ''' python圖片處理 @author:fc_lamp @blog:http://fc-lamp.blog.163.com/ ''' import Image as image #等比例壓縮圖片 def resizeImg(**args): args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75} arg = {} for key in args_key: if key in args: arg[key] = args[key] im = image.open(arg['ori_img']) ori_w,ori_h = im.size widthRatio = heightRatio = None ratio = 1 if (ori_w and ori_w > arg['dst_w']) or (ori_h and ori_h > arg['dst_h']): if arg['dst_w'] and ori_w > arg['dst_w']: widthRatio = float(arg['dst_w']) / ori_w #正確獲取小數(shù)的方式 if arg['dst_h'] and ori_h > arg['dst_h']: heightRatio = float(arg['dst_h']) / ori_h if widthRatio and heightRatio: if widthRatio < heightRatio: ratio = widthRatio else: ratio = heightRatio if widthRatio and not heightRatio: ratio = widthRatio if heightRatio and not widthRatio: ratio = heightRatio newWidth = int(ori_w * ratio) newHeight = int(ori_h * ratio) else: newWidth = ori_w newHeight = ori_h im.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q']) ''' image.ANTIALIAS還有如下值: NEAREST: use nearest neighbour BILINEAR: linear interpolation in a 2x2 environment BICUBIC:cubic spline interpolation in a 4x4 environment ANTIALIAS:best down-sizing filter ''' #裁剪壓縮圖片 def clipResizeImg(**args): args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75} arg = {} for key in args_key: if key in args: arg[key] = args[key] im = image.open(arg['ori_img']) ori_w,ori_h = im.size dst_scale = float(arg['dst_h']) / arg['dst_w'] #目標(biāo)高寬比 ori_scale = float(ori_h) / ori_w #原高寬比 if ori_scale >= dst_scale: #過(guò)高 width = ori_w height = int(width*dst_scale) x = 0 y = (ori_h - height) / 3 else: #過(guò)寬 height = ori_h width = int(height*dst_scale) x = (ori_w - width) / 2 y = 0 #裁剪 box = (x,y,width+x,height+y) #這里的參數(shù)可以這么認(rèn)為:從某圖的(x,y)坐標(biāo)開(kāi)始截,截到(width+x,height+y)坐標(biāo) #所包圍的圖像,crop方法與php中的imagecopy方法大為不一樣 newIm = im.crop(box) im = None #壓縮 ratio = float(arg['dst_w']) / width newWidth = int(width * ratio) newHeight = int(height * ratio) newIm.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q']) #水印(這里僅為圖片水印) def waterMark(**args): args_key = {'ori_img':'','dst_img':'','mark_img':'','water_opt':''} arg = {} for key in args_key: if key in args: arg[key] = args[key] im = image.open(arg['ori_img']) ori_w,ori_h = im.size mark_im = image.open(arg['mark_img']) mark_w,mark_h = mark_im.size option ={'leftup':(0,0),'rightup':(ori_w-mark_w,0),'leftlow':(0,ori_h-mark_h), 'rightlow':(ori_w-mark_w,ori_h-mark_h) } im.paste(mark_im,option[arg['water_opt']],mark_im.convert('RGBA')) im.save(arg['dst_img']) #Demon #源圖片 ori_img = 'D:/tt.jpg' #水印標(biāo) mark_img = 'D:/mark.png' #水印位置(右下) water_opt = 'rightlow' #目標(biāo)圖片 dst_img = 'D:/python_2.jpg' #目標(biāo)圖片大小 dst_w = 94 dst_h = 94 #保存的圖片質(zhì)量 save_q = 35 #裁剪壓縮 clipResizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q = save_q) #等比例壓縮 #resizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q=save_q) #水印 #waterMark(ori_img=ori_img,dst_img=dst_img,mark_img=mark_img,water_opt=water_opt)
總結(jié)
以上就是本文關(guān)于python使用pil進(jìn)行圖像處理(等比例壓縮、裁剪)實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Python基礎(chǔ)學(xué)習(xí)之奇異的GUI對(duì)話框
今天跨進(jìn)了GUI編程的園地,才發(fā)現(xiàn)python語(yǔ)言是這么的好玩,文中對(duì)GUI對(duì)話框作了非常詳細(xì)的介紹,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05Python利用PyExecJS庫(kù)執(zhí)行JS函數(shù)的案例分析
這篇文章主要介紹了Python利用PyExecJS庫(kù)執(zhí)行JS函數(shù),本文通過(guò)案例分析給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12ChatGPT教你用Python實(shí)現(xiàn)BinarySearchTree詳解
這篇文章主要為大家介紹了ChatGPT教你用Python實(shí)現(xiàn)BinarySearchTree詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Python最基本的數(shù)據(jù)類型以及對(duì)元組的介紹
這篇文章主要介紹了Python最基本的數(shù)據(jù)類型以及對(duì)元組的介紹,來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04python pandas 對(duì)series和dataframe的重置索引reindex方法
今天小編就為大家分享一篇python pandas 對(duì)series和dataframe的重置索引reindex方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06R vs. Python 數(shù)據(jù)分析中誰(shuí)與爭(zhēng)鋒?
R和Python兩者誰(shuí)更適合數(shù)據(jù)分析領(lǐng)域?在某些特定情況下誰(shuí)會(huì)更有優(yōu)勢(shì)?還是一個(gè)天生在各方面都比另一個(gè)更好?2017-10-10Python實(shí)現(xiàn)處理Excel數(shù)據(jù)并生成只讀模式
這篇文章主要為大家詳細(xì)介紹了如何使用 Python 處理 Excel 數(shù)據(jù),并生成只讀模式的 Excel 文檔,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以參考下2023-11-11