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

python實現批量修改圖片格式和尺寸

 更新時間:2018年06月07日 11:01:58   作者:PassionY  
這篇文章主要為大家詳細介紹了python實現批量修改圖片格式和尺寸的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

公司的一個項目要求把所有4096x4096的圖片全部轉化成2048x2048的圖片,這種批量轉換圖片大小的軟件網上很多,我的同事原來使用的美圖看看的批量轉換,但是稍微有點麻煩,每次還需要指定要轉換的圖片的輸入路徑和輸出路徑,而且每次都只能處理一個文件夾,很繁瑣,于是我想到了萬能的Python,然后寫了一個腳本來批量處理圖片,同一個根目錄下的所有文件夾的子文件等的圖片全部會處理掉。

代碼中還加入了很多的異常捕獲機制和提示,希望對大家有幫助。

備注:

1.導入了PIL庫,是處理圖片用的,很強大;

2.導入了win32庫,是判斷隱藏文件用的,我們的項目需要刪除隱藏文件,不需要的可以直接找到刪除。

3.導入send2trash庫,是把刪除的文件放進垃圾箱,而不是永久刪除,這個我只是防止刪除有用的文件而搞得,有點嚴謹了是吧,不需要的可以刪掉啊。

4.我這個腳本是Python2.7編寫的,但是在處理中文編碼的時候非常惡心,盡管最后被我解決了,這個解決的方法,我隨后會再單獨寫一篇,但是此刻我是建議大家不要用2.x版本的python 了。據說3.x的版本的已經解決了編碼的問題。希望大家聽我的建議。

#coding=utf-8 
import sys 
import os, glob 
import platform 
import win32file,win32con 
from PIL import Image 
from send2trash import send2trash 
 
reload(sys) 
sys.setdefaultencoding('utf-8') 
 
#new_width =2048 
#width =int(raw_input("the width U want:")) 
#imgslist = glob.glob(path+'/*.*') 
 
ShuiPing="水平" 
ShiZhuang="矢狀" 
GuanZhuang="冠狀" 
 
def Py_Log(_string): 
  print "----"+_string.decode('utf-8')+"----" 
 
def is_windows_system(): 
  return 'Windows' in platform.system() 
 
def is_hiden_file(file_Path):  
  if is_windows_system():  
    fileAttr = win32file.GetFileAttributes(file_Path) 
    if fileAttr & win32con.FILE_ATTRIBUTE_HIDDEN :  
      return True  
    return False  
  return False 
 
def remove_hidden_file(file_path): 
  send2trash(file_path) 
  print "Delete hidden file path:"+file_path 
 
def astrcmp(str1,str2): 
  return str1.lower()==str2.lower() 
 
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 change_img_type(img_path): 
  try: 
    img = Image.open(img_path) 
    img.save('new_type.png') 
  except Exception,e: 
    print e 
 
#處理遠程圖片 
def handle_remote_img(img_url): 
  try: 
    request = urllib2.Request(img_url) 
    img_data = urllib2.urlopen(request).read() 
    img_buffer = StringIO.StringIO(img_data) 
    img = Image.open(img_buffer) 
    img.save('remote.jpg') 
    (width,height) = img.size 
    out = img.resize((200,height * 200 / width),Image.ANTIALIAS) 
    out.save('remote_small.jpg') 
  except Exception,e: 
    print e 
 
def rename_forder(forder_path): 
  Py_Log("------------rename_forder--------------------------") 
  names = os.path.split(forder_path) 
  try: 
    if(unicode(ShuiPing) in unicode(names[1],'gbk')): 
      os.rename(forder_path,names[0]+"\\"+"01") 
      Py_Log(names[1]+"-->"+"01") 
    if(unicode(ShiZhuang) in unicode(names[1],'gbk')): 
      os.rename(forder_path,names[0]+"\\"+"02") 
      Py_Log(names[1]+"-->"+"02") 
    if(unicode(GuanZhuang) in unicode(names[1],'gbk')): 
      os.rename(forder_path,names[0]+"\\"+"03") 
      Py_Log(names[1]+"-->"+"03") 
  except Exception,e: 
    print e 
 
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 
 
def DFS_Dir(dirPath, dirCallback = None, fileCallback = None): 
  stack = [] 
  ret = [] 
  stack.append(dirPath); 
  while len(stack) > 0: 
    tmp = stack.pop(len(stack) - 1) 
    if(os.path.isdir(tmp)): 
      ret.append(tmp) 
      for item in os.listdir(tmp): 
        stack.append(os.path.join(tmp, item)) 
      if dirCallback: 
        dirCallback(tmp) 
    elif(os.path.isfile(tmp)): 
      ret.append(tmp) 
      if fileCallback: 
        fileCallback(tmp) 
  return ret 
 
def printDir(dirPath): 
  print "dir: " + dirPath 
  if(is_hiden_file(dirPath)): 
    remove_hidden_file(dirPath) 
  else: 
    rename_forder(dirPath) 
 
def printFile(dirPath): 
  print "file: " + dirPath 
  resize_image(dirPath) 
  return True 
 
 
if __name__ == '__main__': 
  while True: 
    path = raw_input("Path:") 
    new_width =int(raw_input("the width U want:")) 
    try: 
      b = BFS_Dir(path , printDir, printFile) 
      Py_Log ("\r\n   **********\r\n"+"*********圖片處理完畢*********"+"\r\n  **********\r\n") 
    except: 
      print "Unexpected error:", sys.exc_info() 
    raw_input('press enter key to rehandle') 

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

相關文章

  • Python使用multiprocessing實現一個最簡單的分布式作業(yè)調度系統

    Python使用multiprocessing實現一個最簡單的分布式作業(yè)調度系統

    mutilprocess像線程一樣管理進程,這個是mutilprocess的核心,他與threading很是相像,對多核CPU的利用率會比threading好的多,通過本文給大家介紹Python使用multiprocessing實現一個最簡單的分布式作業(yè)調度系統,需要的朋友參考下
    2016-03-03
  • pytorch三層全連接層實現手寫字母識別方式

    pytorch三層全連接層實現手寫字母識別方式

    今天小編就為大家分享一篇pytorch三層全連接層實現手寫字母識別方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python3 JSON編碼解碼方法詳解

    Python3 JSON編碼解碼方法詳解

    這篇文章主要介紹了Python3 JSON編碼解碼方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • 序列化Python對象的方法

    序列化Python對象的方法

    這篇文章主要介紹了序列化Python對象的方法,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-08-08
  • Python?re模塊的使用全過程

    Python?re模塊的使用全過程

    這篇文章主要介紹了Python?re模塊的使用全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • django實現圖片上傳數據庫并顯示

    django實現圖片上傳數據庫并顯示

    這篇文章主要為大家詳細介紹了django實現圖片上傳數據庫并顯示,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • numpy拼接矩陣的實現

    numpy拼接矩陣的實現

    本文主要介紹了numpy拼接矩陣的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • 詳細介紹Python函數中的默認參數

    詳細介紹Python函數中的默認參數

    這篇文章主要介紹了詳細介紹Python函數中的默認參數,包括默認參數的傳遞和求值等內容,需要的朋友可以參考下
    2015-03-03
  • python twilio模塊實現發(fā)送手機短信功能

    python twilio模塊實現發(fā)送手機短信功能

    這篇文章主要介紹了python twilio模塊實現發(fā)送手機短信的功能,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • Python微信公眾號開發(fā)平臺

    Python微信公眾號開發(fā)平臺

    這篇文章主要介紹了Python微信公眾號開發(fā)平臺,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2018-01-01

最新評論

广德县| 湖南省| 获嘉县| 惠东县| 射洪县| 延津县| 蒙阴县| 贡觉县| 天津市| 玛曲县| 汉寿县| 昌平区| 靖远县| 东莞市| 万州区| 博爱县| 北流市| 罗江县| 无棣县| 满洲里市| 环江| 屏山县| 边坝县| 翼城县| 桂阳县| 班玛县| 平湖市| 景德镇市| 喜德县| 若羌县| 海原县| 石泉县| 山丹县| 竹溪县| 登封市| 贵德县| 贡嘎县| 莎车县| 兴化市| 桐乡市| 恩平市|