久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

python實(shí)現(xiàn)快速文件格式批量轉(zhuǎn)換的方法

瀏覽:65日期:2022-07-08 09:18:39

用python實(shí)現(xiàn)文件夾下的成批文件格式轉(zhuǎn)換

我們對于文件轉(zhuǎn)換的需求很大,甚至于對于圖片的格式,JPG和PNG格式在肉眼看來都沒什么差別,但是對于計(jì)算機(jī)而言,它有時(shí)候就只接受這些肉眼看起來差不多的格式的其中一種。

環(huán)境

windows10python3.7+pycharm

創(chuàng)建目錄

1.在編程前,創(chuàng)建一個(gè)文件夾,并放入你想用的文件(非目錄),這些文件的格式不合適。例如,我在桌面創(chuàng)建了名為'in_path'的文件夾,在里面放進(jìn)了.pgm和.png格式的文件,想讓他們都轉(zhuǎn)化成.jpg格式。2.同時(shí)新建一個(gè)batch_change.py文件。

python實(shí)現(xiàn)快速文件格式批量轉(zhuǎn)換的方法

編寫程序

導(dǎo)入python的模塊os,PIL,glob.

// 導(dǎo)入PIL,os,globfrom PIL import Imageimport os,glob

創(chuàng)建輸出目錄

// 創(chuàng)建輸出文件夾def batch_change(in_path,out_path): if not os.path.exists(out_path): print(out_path,’is not existed.’) os.mkdir(out_path) if not os.path.exists(in_path): print(in_path,’is not existed.’) return -1

瀏覽輸入目錄

// 瀏覽遍歷輸入文件夾 for files in glob.glob(in_path+’/*’): filepath,filename=os.path.split(files) out_file = filename[0:9]+’.jpg’ #轉(zhuǎn)換成最終格式為.jpg,可以在這里改為.png im = Image.open(files) new_path=os.path.join(out_path,out_file) print(count,’,’,new_path) count = count+1 im.save(os.path.join(out_path,out_file))

修改文件路徑

// 瀏覽遍歷輸入文件夾 if __name__==’__main__’: batch_change(r’C:Users80610Desktopin_path’,r’C:Users80610Desktopout_path’) #你想轉(zhuǎn)化文件所在文件夾輸入和輸出的路徑

運(yùn)行結(jié)果

無論是pgm,png,他們們都轉(zhuǎn)化成.jpg格式,并且保存在out_path文件夾下

python實(shí)現(xiàn)快速文件格式批量轉(zhuǎn)換的方法

python實(shí)現(xiàn)快速文件格式批量轉(zhuǎn)換的方法

完整代碼

#encoding = utf-8#author = itinerary,huifrom PIL import Imageimport os,globdef batch_change(in_path,out_path): #參數(shù):輸入與輸出文件夾路徑 if not os.path.exists(out_path): print(out_path,’is not existed.’) #創(chuàng)建輸出文件夾 os.mkdir(out_path) if not os.path.exists(in_path): print(in_path,’is not existed.’) return -1 count = 0 for files in glob.glob(in_path+’/*’): filepath,filename=os.path.split(files) out_file = filename[0:9]+’.png’ #轉(zhuǎn)換成最終格式為png im = Image.open(files) new_path=os.path.join(out_path,out_file) print(count,’,’,new_path) count = count+1 im.save(os.path.join(out_path,out_file))if __name__==’__main__’: batch_change(r’C:Users80610Desktopin_path’,r’C:Users80610Desktopout_path’) #你想轉(zhuǎn)化文件所在文件夾輸入和輸出的路近

總結(jié)

到此這篇關(guān)于python實(shí)現(xiàn)快速文件格式批量轉(zhuǎn)換的方法的文章就介紹到這了,更多相關(guān)python文件格式批量轉(zhuǎn)換內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产精品久久久久婷婷二区次 | av毛片| 黄色毛片在线看 | 九九色影院 | 国产日产欧美a级毛片 | 日本黄色三级网站 | 成人 在线| 欧美在线网站 | 久久久国产视频 | 精品福利av导航 | 久久久亚洲成人 | 中国黄色在线视频 | 在线观看中文 | 99久久婷婷| 国产成人aⅴ | 欧美在线一区二区 | 欧美精品乱码久久久久久按摩 | 久久九九这里只有精品 | 成人亚洲视频在线观看 | 国产精品视频入口 | 国产精品免费观看 | 欧美三级视频 | 亚洲国产日韩在线 | 国产在线二区 | 91精品综合久久久久久五月天 | 特黄特黄aaaa级毛片免费看 | 国产特级毛片aaaaaa毛片 | 欧美在线a | av日韩在线看 | 午夜影院久久 | 日韩欧美精品在线 | 羞羞视频在线观免费观看 | 伊人久操| 91在线资源 | 久草免费在线 | 特黄一级 | 北条麻妃国产九九九精品小说 | 免费大片在线观看网站 | 一本一道久久精品综合 | 夜本色 | 欧美精品一区二区三区四区在线 |