python實(shí)現(xiàn)對圖片進(jìn)行旋轉(zhuǎn),放縮,裁剪的功能
先說明下,我這是對某個目錄下的圖片名稱進(jìn)行操作,該目錄下的圖片名稱為1.jpg,2.jpg。。。。。這樣類似的圖片名。
1.旋轉(zhuǎn)
# -*-coding:utf-8-*- from PIL import Image def rotateimg(inputimg,outimg): im = Image.open(inputimg) # 圖片的寬度和高度 img_size = im.size print("圖片寬度和高度分別是{}".format(img_size)) # 旋轉(zhuǎn)圖片 # 左旋轉(zhuǎn)90度 im = im.rotate(90) print("圖片寬度和高度分別是{}".format(im.size)) im.save(outimg) if __name__ == "__main__": # inputpath = "D:/test/tmp/shuibiao/" outpath = "D:/test/tmp/img/" for i in range(1,24): tmpin = inputpath+str(i)+'.jpg' tmpout = outpath+str(i)+'.jpg' rotateimg(tmpin, tmpout) print('成功旋轉(zhuǎn)第'+str(i)+'張圖片')
2.放縮
# -*-coding:utf-8-*- from PIL import Image def resizeimg(inputimg,outimg): img1 = Image.open(inputimg) out=img1.resize((260, 180),Image.ANTIALIAS) print("圖片寬度和高度分別是{}".format(out.size)) out.save(outimg) if __name__ == "__main__": # inputpath = "D:/test/tmp/img/" outpath = "D:/test/tmp/img_1/" for i in range(1, 24): tmpin = inputpath + str(i) + '.jpg' tmpout = outpath + str(i) + '.jpg' resizeimg(tmpin, tmpout) print('成功放縮第' + str(i) + '張圖片')
3.裁剪
# -*-coding:utf-8-*- from PIL import Image def cropimg(inputimg,outimg): im = Image.open(inputimg) # 圖片的寬度和高度 img_size = im.size print("圖片寬度和高度分別是{}".format(img_size)) ''' 裁剪:傳入一個元組作為參數(shù) 元組里的元素分別是:(距離圖片左邊界距離x, 距離圖片上邊界距離y,距離圖片左邊界距離+裁剪框?qū)挾葂+w,距離圖片上邊界距離+裁剪框高度y+h) ''' # 截取圖片中一塊寬和高都是250的 x = 0 y = 60 w = 260 h = 60 region = im.crop((x, y, x + w, y + h)) region.save(outimg) if __name__ == "__main__": inputpath = "D:/test/tmp/img_1/" outpath = "D:/test/tmp/img_2/" for i in range(1, 24): tmpin = inputpath + str(i) + '.jpg' tmpout = outpath + str(i) + '.jpg' cropimg(tmpin, tmpout) print('成功裁剪第' + str(i) + '張圖片')
以上這篇python實(shí)現(xiàn)對圖片進(jìn)行旋轉(zhuǎn),放縮,裁剪的功能就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于python實(shí)現(xiàn)計(jì)算兩組數(shù)據(jù)P值
這篇文章主要介紹了基于python實(shí)現(xiàn)計(jì)算兩組數(shù)據(jù)P值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07聊聊Python pandas 中l(wèi)oc函數(shù)的使用,及跟iloc的區(qū)別說明
這篇文章主要介紹了聊聊Python pandas 中l(wèi)oc函數(shù)的使用,及跟iloc的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03python中np.random.permutation函數(shù)實(shí)例詳解
np.random.permutation是numpy中的一個函數(shù),它可以將一個數(shù)組中的元素隨機(jī)打亂,返回一個打亂后的新數(shù)組,下面這篇文章主要給大家介紹了關(guān)于python中np.random.permutation函數(shù)的相關(guān)資料,需要的朋友可以參考下2023-04-04Python微服務(wù)開發(fā)之使用FastAPI構(gòu)建高效API
微服務(wù)架構(gòu)在現(xiàn)代軟件開發(fā)中日益普及,它將復(fù)雜的應(yīng)用程序拆分成多個可獨(dú)立部署的小型服務(wù)。本文將介紹如何使用 Python 的 FastAPI 庫快速構(gòu)建和部署微服務(wù),感興趣的可以了解一下2023-05-05復(fù)習(xí)Python中的字符串知識點(diǎn)
這篇文章主要介紹了Python中字符串的一些知識點(diǎn),來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04