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

首頁 > 編程 > Python > 正文

python批量修改圖片大小的方法

2020-01-04 14:44:32
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python批量修改圖片大小的具體代碼,供大家參考,具體內容如下

引用的模塊

from PIL import Image

Image的使用

def resize_image(img_path):  try:    mPath, ext = os.path.splitext(img_path)    if astrcmp(ext, ".png") or astrcmp(ext, ".jpg"):      img = Image.open(img_path)      (width, height) = img.size      if width != new_width:        new_height = int(height * new_width / width)        out = img.resize((new_width, new_height), Image.ANTIALIAS)        new_file_name = '%s%s' % (mPath, ext)        out.save(new_file_name, quality=100)        Py_Log("圖片尺寸修改為:" + str(new_width))      else:        Py_Log("圖片尺寸正確,未修改")    else:      Py_Log("非圖片格式")  except Exception, e:    print edef printFile(dirPath):  print "file: " + dirPath  resize_image(dirPath)  return True

引用

if __name__ == '__main__':  path = "E:/pp/icon_setting.png"  new_width = 50  try:    b = printFile(path)    Py_Log("/r/n     **********/r/n" + "*********圖片處理完畢*********" + "/r/n     **********/r/n")  except:    print "Unexpected error:", sys.exc_info()

上述是修改單一的圖片,若要批量修改文件夾下的所有圖片,則要使用循環,在上面基礎添加 例如:

def BFS_Dir(dirPath, dirCallback=None, fileCallback=None):  queue = []  ret = []  queue.append(dirPath);  while len(queue) > 0:    tmp = queue.pop(0)    if os.path.isdir(tmp):      ret.append(tmp)      for item in os.listdir(tmp):        queue.append(os.path.join(tmp, item))      if dirCallback:        dirCallback(tmp)    elif os.path.isfile(tmp):      ret.append(tmp)      if fileCallback:        fileCallback(tmp)  return ret

第一個參數為圖片的目錄路徑,第二個參數是(目錄路勁的回掉方法),第三個參數是圖片處理回掉方法

源代碼參考:Python_Tool

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙井市| 吐鲁番市| 于都县| 清镇市| 乐清市| 霍林郭勒市| 景宁| 仁化县| 永吉县| 安平县| 谢通门县| 敖汉旗| 长岛县| 长沙县| 蒙自县| 营山县| 云浮市| 安福县| 永康市| 关岭| 榕江县| 遵义县| 兴海县| 海南省| 南陵县| 永善县| 芷江| 南平市| 巧家县| 洛扎县| 江达县| 板桥市| 报价| 无锡市| 太和县| 原平市| 南宁市| 九寨沟县| 齐齐哈尔市| 恩施市| 梓潼县|