国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄石市| 常山县| 江城| 东方市| 富锦市| 岗巴县| 香港| 木兰县| 宜春市| 旌德县| 祥云县| 永靖县| 台南县| 延寿县| 西青区| 保定市| 宜城市| 思茅市| 临高县| 开鲁县| 虎林市| 略阳县| 深圳市| 灌云县| 红桥区| 虞城县| 区。| 邵东县| 墨竹工卡县| 临潭县| 江华| 金秀| 博湖县| 舒城县| 明溪县| 临洮县| 饶河县| 石棉县| 靖宇县| 兴安县| 宁强县|