1. 程式人生 > >Python批量修改圖片格式和尺寸

Python批量修改圖片格式和尺寸

click alias cli 項目 重新 ide urllib 文本 直接

Python批量修改圖片格式和尺寸

備註:

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

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

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

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

[python] 查看純文本
  1. #編碼= utf-8的
  2. 導入 系統
  3. 導入 os,glob
  4. 導入 平臺
  5. 導入 win32file,win32con
  6. PIL 導入 圖像
  7. send2trash 導入 send2trash
  8. 重裝(SYS)
  9. sys.setdefaultencoding(‘utf-8‘
  10. #new_width = 2048
  11. #width = int(raw_input(“width U want:”))
  12. #imgslist = glob.glob(路徑+‘/ *。*‘)
  13. ShuiPing =
    “水平”
  14. ShiZhuang = “矢狀”
  15. GuanZhuang = “冠狀”
  16. def Py_Log(_string):
  17. 打印“----” + _string.decode(‘utf-8‘ )+ “----”
  18. def is_windows_system():
  19. 返回“窗口” platform.system()
  20. def is_hiden_file(file_Path):
  21. 如果 is_windows_system():
  22. fileAttr = win32file.GetFileAttributes(file_Path)
  23. 如果 fileAttr&win32con.FILE_ATTRIBUTE_HIDDEN:
  24. 返回True
  25. 返回False
  26. 返回False
  27. def remove_hidden_??file(file_path):
  28. send2trash(FILE_PATH)
  29. 打印“刪除隱藏的文件路徑:” + file_path
  30. def astrcmp(str1,str2):
  31. 返回 str1.lower()== str2.lower()
  32. def resize_image(img_path):
  33. 嘗試
  34. mPath,ext = os.path.splitext(img_path)
  35. 如果 (astrcmp(ext,“. png astrcmp(ext,“.jpg” )):
  36. img = Image.open(img_path)
  37. (寬度,高度)= img.size
  38. if (width!= new_width):
  39. new_height = int(height * new_width / width)
  40. out = img.resize((new_width,new_height),Image.ANTIALIAS)
  41. new_file_name = ‘%s%s‘ %(mPath,ext)
  42. out.save(new_file_name,質量= 100
  43. Py_Log(“圖片尺寸修改為:” + str(new_width))
  44. 其他
  45. Py_Log(“圖片尺寸正確,未修改”
  46. 其他
  47. Py_Log(“非圖片格式”
  48. 除了 例外,e:
  49. 打印 e
  50. #改變圖片類型
  51. def change_img_type(img_path):
  52. 嘗試
  53. img = Image.open(img_path)
  54. img.save(‘new_type.png‘
  55. 除了 例外,e:
  56. 打印 e
  57. #處理遠程圖片
  58. def handle_remote_img(img_url):
  59. 嘗試
  60. request = urllib2.Request(img_url)
  61. img_data = urllib2.urlopen(request).read()
  62. img_buffer = StringIO.StringIO(img_data)
  63. img = Image.open(img_buffer)
  64. img.save(‘remote.jpg‘
  65. (寬度,高度)= img.size
  66. out = img.resize((200 ,height * 200 / width),Image.ANTIALIAS)
  67. out.save(‘remote_small.jpg‘
  68. 除了 例外,e:
  69. 打印 e
  70. def rename_forder(forder_path):
  71. Py_Log(“------------ rename_forder --------------------------”
  72. names = os.path.split(forder_path)
  73. 嘗試
  74. 如果(UNICODE(熱水瓶) 的Unicode(名稱[ 1 ],‘GBK‘ )):
  75. os.rename(forder_path,名稱[ 0 ] + “\\”+“ 01 ”)
  76. Py_Log(名稱[ 1 ] + “ - >” + “01”
  77. 如果(unicode(石莊) in unicode(names [ 1 ],‘gbk‘ )):
  78. os.rename(forder_path,名稱[ 0 ] + “\\”+“ 02 ”)
  79. Py_Log(名稱[ 1 ] + “ - >” + “02”
  80. 如果(unicode(管莊) in unicode(names [ 1 ],‘gbk‘ )):
  81. os.rename(forder_path,名稱[ 0 ] + “\\”+“ 03 ”)
  82. Py_Log(名稱[ 1 ] + “ - >” + “03”
  83. 除了 例外,e:
  84. 打印 e
  85. def BFS_Dir(dirPath,dirCallback = None ,fileCallback = None ):
  86. 隊列= []
  87. ret = []
  88. queue.append(dirPath);
  89. len(隊列)> 0
  90. tmp = queue.pop(0
  91. 如果(os.path.isdir(tmp)):
  92. ret.append(TMP)
  93. 對於 項目 os.listdir(TMP):
  94. queue.append(os.path.join(tmp,item))
  95. 如果 dirCallback:
  96. dirCallback(TMP)
  97. elif (os.path.isfile(tmp)):
  98. ret.append(TMP)
  99. 如果 fileCallback:
  100. fileCallback(TMP)
  101. 返回 RET
  102. def DFS_Dir(dirPath,dirCallback = None ,fileCallback = None ):
  103. stack = []
  104. ret = []
  105. stack.append(dirPath);
  106. len(stack)> 0
  107. tmp = stack.pop(len(stack) - 1
  108. 如果(os.path.isdir(tmp)):
  109. ret.append(TMP)
  110. 對於 項目 os.listdir(TMP):
  111. stack.append(os.path.join(tmp,item))
  112. 如果 dirCallback:
  113. dirCallback(TMP)
  114. elif (os.path.isfile(tmp)):
  115. ret.append(TMP)
  116. 如果 fileCallback:
  117. fileCallback(TMP)
  118. 返回 RET
  119. def printDir(dirPath):
  120. 打印“dir:” + dirPath
  121. if (is_hiden_file(dirPath)):
  122. remove_hidden_??file(dirPath)
  123. 其他
  124. rename_forder(dirPath)
  125. def printFile(dirPath):
  126. 打印“文件:” + dirPath
  127. resize_image(dirPath)
  128. 返回True
  129. 如果 __name__ == __main__
  130. path = raw_input(“Path:”
  131. new_width = int(raw_input(“width U want:” ))
  132. 嘗試
  133. b = BFS_Dir(path,printDir,printFile)
  134. Py_Log(“\ r \ n ********** \ r \ n” + “*********圖片處理完畢*********” + “\ r \ n ********** \ r \ n“
  135. 除了
  136. 打印“意外的錯誤:” ,sys.exc_info()
  137. raw_input(‘按回車鍵重新處理‘

Python批量修改圖片格式和尺寸