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

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

 更新時(shí)間:2020年10月16日 10:13:37   作者:itinerary,hui  
這篇文章主要介紹了python實(shí)現(xiàn)快速文件格式批量轉(zhuǎn)換的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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

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

環(huán)境

windows10
python3.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文件。

在這里插入圖片描述

編寫程序

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

// 導(dǎo)入PIL,os,glob
from PIL import Image
import 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:\Users\80610\Desktop\in_path',r'C:\Users\80610\Desktop\out_path') 
  #你想轉(zhuǎn)化文件所在文件夾輸入和輸出的路徑

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

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

在這里插入圖片描述

在這里插入圖片描述

完整代碼

#encoding = utf-8
#author = itinerary,hui

from PIL import Image
import os,glob

def 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:\Users\80610\Desktop\in_path',r'C:\Users\80610\Desktop\out_path') #你想轉(zhuǎn)化文件所在文件夾輸入和輸出的路近

總結(jié)

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

相關(guān)文章

最新評(píng)論

盱眙县| 扎兰屯市| 扶风县| 霍邱县| 林周县| 东至县| 翁牛特旗| 子洲县| 宝兴县| 祁门县| 龙州县| 庐江县| 安乡县| 卓资县| 祁阳县| 维西| 铜陵市| 西乡县| 南汇区| 普安县| 新河县| 柘荣县| 宽甸| 东至县| 铁力市| 隆回县| 弥渡县| 读书| 巨野县| 马公市| 莫力| 凌云县| 福泉市| 久治县| 油尖旺区| 宜川县| 泽普县| 永宁县| 大兴区| 和田市| 滨州市|