最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年07月24日 15:18:38   作者:Lee-ldc  
這篇文章主要為大家詳細(xì)介紹了python批量修改圖片大小的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

引用的模塊

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 e

def 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()

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

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

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

源代碼參考:Python_Tool

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

潞城市| 苍梧县| 高邑县| 金湖县| 石阡县| 盘山县| 专栏| 玉门市| 千阳县| 遂宁市| 德江县| 婺源县| 泸西县| 临漳县| 乌兰浩特市| 休宁县| 浦县| 永昌县| 株洲县| 苍梧县| 防城港市| 安塞县| 炉霍县| 惠东县| 田阳县| 黄平县| 金川县| 新乡县| 尚志市| 高邑县| 若尔盖县| 漳浦县| 中牟县| 齐齐哈尔市| 班玛县| 西林县| 天等县| 苍南县| 仪陇县| 新源县| 钟祥市|