国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現批量修改圖片格式和大小的方法【opencv庫與PIL庫】

2020-02-15 23:53:30
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現批量修改圖片格式和大小的方法。分享給大家供大家參考,具體如下:

第一種方法用到opencv庫

import osimport timeimport cv2def alter(path,object):  result = []  s = os.listdir(path)  count = 1  for i in s:    document = os.path.join(path,i)    img = cv2.imread(document)    img = cv2.resize(img, (20,20))    listStr = [str(int(time.time())), str(count)]    fileName = ''.join(listStr)    cv2.imwrite(object+os.sep+'%s.jpg' % fileName, img)    count = count + 1alter('C://imgDemo','C://imgDemo1')

第二種方法用到PIL庫

import osimport timefrom PIL import Imagedef alter(path,object):  s = os.listdir(path)  count = 1  for i in s:    document = os.path.join(path,i)    img = Image.open(document)    out = img.resize((20,20))    listStr = [str(int(time.time())), str(count)]    fileName = ''.join(listStr)    out.save(object+os.sep+'%s.jpg' % fileName)    count = count + 1alter('C://imgDemo','C://imgDemo1')

運行上述代碼可得到C:/imgDemo目錄下對應批量生成的20*20大小的圖片。

運行效果如下:

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玛多县| 侯马市| 青阳县| 靖宇县| 慈溪市| 卢龙县| 平远县| 柘城县| 东光县| 临颍县| 合江县| 天祝| 铁力市| 谢通门县| 东安县| 怀集县| 扶风县| 靖江市| 新绛县| 安国市| 南溪县| 分宜县| 噶尔县| 中牟县| 西宁市| 张家港市| 忻州市| 绍兴市| 锡林郭勒盟| 双桥区| 罗甸县| 南漳县| 长宁县| 上蔡县| 方山县| 五指山市| 明溪县| 新昌县| 景泰县| 柳州市| 新竹县|