Python批量修改圖片格式和尺寸
阿新 • • 發佈:2017-12-25
click alias cli 項目 重新 ide urllib 文本 直接
Python批量修改圖片格式和尺寸
備註:
1.導入了PIL庫,是處理圖片用的,很強大;
2.導入了的win32庫,是判斷隱藏文件用的,我們的項目需要刪除隱藏文件,不需要的可以直接找到刪除。
3.導入send2trash庫,是把刪除的文件放進垃圾箱,而不是永久刪除,這個我只是防止刪除有用的文件而搞得,有點嚴謹了是吧,不需要的可以刪掉啊。
4.我這個腳本是Python2.7編寫的,但是在處理中文編碼的時候非常惡心,盡管最後被我解決了,這個解決的方法,我隨後會再單獨寫一篇,但是此刻我是建議大家不要用2.x版本的python了。據說3.x的版本的已經解決了編碼的問題。希望大家聽我的建議。
[python] 查看純文本
- #編碼= utf-8的
- 導入 系統
- 導入 os,glob
- 導入 平臺
- 導入 win32file,win32con
- 從 PIL 導入 圖像
- 從 send2trash 導入 send2trash
- 重裝(SYS)
- sys.setdefaultencoding(‘utf-8‘ )
- #new_width = 2048
- #width = int(raw_input(“width U want:”))
- #imgslist = glob.glob(路徑+‘/ *。*‘)
- ShuiPing =
- ShiZhuang = “矢狀”
- GuanZhuang = “冠狀”
- def Py_Log(_string):
- 打印“----” + _string.decode(‘utf-8‘ )+ “----”
- def is_windows_system():
- 返回“窗口” 在 platform.system()
- def is_hiden_file(file_Path):
- 如果 is_windows_system():
- fileAttr = win32file.GetFileAttributes(file_Path)
- 如果 fileAttr&win32con.FILE_ATTRIBUTE_HIDDEN:
- 返回True
- 返回False
- 返回False
- def remove_hidden_??file(file_path):
- send2trash(FILE_PATH)
- 打印“刪除隱藏的文件路徑:” + file_path
- def astrcmp(str1,str2):
- 返回 str1.lower()== str2.lower()
- def resize_image(img_path):
- 嘗試:
- mPath,ext = os.path.splitext(img_path)
- 如果 (astrcmp(ext,“. png ” ) 或astrcmp(ext,“.jpg” )):
- img = Image.open(img_path)
- (寬度,高度)= 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,質量= 100 )
- Py_Log(“圖片尺寸修改為:” + str(new_width))
- 其他:
- Py_Log(“圖片尺寸正確,未修改” )
- 其他:
- Py_Log(“非圖片格式” )
- 除了 例外,e:
- 打印 e
- #改變圖片類型
- def change_img_type(img_path):
- 嘗試:
- img = Image.open(img_path)
- img.save(‘new_type.png‘ )
- 除了 例外,e:
- 打印 e
- #處理遠程圖片
- def handle_remote_img(img_url):
- 嘗試:
- 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‘ )
- (寬度,高度)= img.size
- out = img.resize((200 ,height * 200 / width),Image.ANTIALIAS)
- out.save(‘remote_small.jpg‘ )
- 除了 例外,e:
- 打印 e
- def rename_forder(forder_path):
- Py_Log(“------------ rename_forder --------------------------” )
- names = os.path.split(forder_path)
- 嘗試:
- 如果(UNICODE(熱水瓶) 中 的Unicode(名稱[ 1 ],‘GBK‘ )):
- os.rename(forder_path,名稱[ 0 ] + “\\”+“ 01 ”)
- Py_Log(名稱[ 1 ] + “ - >” + “01” )
- 如果(unicode(石莊) in unicode(names [ 1 ],‘gbk‘ )):
- os.rename(forder_path,名稱[ 0 ] + “\\”+“ 02 ”)
- Py_Log(名稱[ 1 ] + “ - >” + “02” )
- 如果(unicode(管莊) in unicode(names [ 1 ],‘gbk‘ )):
- os.rename(forder_path,名稱[ 0 ] + “\\”+“ 03 ”)
- Py_Log(名稱[ 1 ] + “ - >” + “03” )
- 除了 例外,e:
- 打印 e
- def BFS_Dir(dirPath,dirCallback = None ,fileCallback = None ):
- 隊列= []
- ret = []
- queue.append(dirPath);
- 而 len(隊列)> 0 :
- tmp = queue.pop(0 )
- 如果(os.path.isdir(tmp)):
- ret.append(TMP)
- 對於 項目 在 os.listdir(TMP):
- queue.append(os.path.join(tmp,item))
- 如果 dirCallback:
- dirCallback(TMP)
- elif (os.path.isfile(tmp)):
- ret.append(TMP)
- 如果 fileCallback:
- fileCallback(TMP)
- 返回 RET
- def DFS_Dir(dirPath,dirCallback = None ,fileCallback = None ):
- stack = []
- ret = []
- stack.append(dirPath);
- 而 len(stack)> 0 :
- tmp = stack.pop(len(stack) - 1 )
- 如果(os.path.isdir(tmp)):
- ret.append(TMP)
- 對於 項目 在 os.listdir(TMP):
- stack.append(os.path.join(tmp,item))
- 如果 dirCallback:
- dirCallback(TMP)
- elif (os.path.isfile(tmp)):
- ret.append(TMP)
- 如果 fileCallback:
- fileCallback(TMP)
- 返回 RET
- def printDir(dirPath):
- 打印“dir:” + dirPath
- if (is_hiden_file(dirPath)):
- remove_hidden_??file(dirPath)
- 其他:
- rename_forder(dirPath)
- def printFile(dirPath):
- 打印“文件:” + dirPath
- resize_image(dirPath)
- 返回True
- 如果 __name__ == ‘ __main__ ‘ :
- 而真:
- path = raw_input(“Path:” )
- new_width = int(raw_input(“width U want:” ))
- 嘗試:
- b = BFS_Dir(path,printDir,printFile)
- Py_Log(“\ r \ n ********** \ r \ n” + “*********圖片處理完畢*********” + “\ r \ n ********** \ r \ n“ )
- 除了:
- 打印“意外的錯誤:” ,sys.exc_info()
- raw_input(‘按回車鍵重新處理‘ )
Python批量修改圖片格式和尺寸