python 빠 른 파일 형식 일괄 변환 방법

python 으로 폴 더 의 일괄 파일 형식 변환 을 실현 합 니 다.
우 리 는 파일 변환 에 대한 수요 가 매우 크다.심지어 그림 의 형식,JPG 와 PNG 형식 은 육안 으로 볼 때 차이 가 없 지만 컴퓨터 로 서 는 육안 으로 보기 에는 차이 가 많 지 않 은 형식 중 하나 만 받 아들 일 때 가 있다.
환경.
windows10
python3.7+pycharm
디 렉 터 리 만 들 기
1.프로 그래 밍 전에 폴 더 를 만 들 고 사용 하고 싶 은 파일(디 렉 터 리 가 아 닌)을 넣 습 니 다.이 파일 들 의 형식 이 맞지 않 습 니 다.
예 를 들 어,나 는 데스크 톱 에'in'이라는 이름 을 만 들 었 다.path"의 폴 더 는.pgm 와.png 형식의 파일 을 넣 어 모두.jpg 형식 으로 바 꾸 려 고 합 니 다.
2.동시에 batch 를 새로 만 듭 니 다.change.py 파일.
在这里插入图片描述
프로그램 을 작성 하 다
python 모듈 가 져 오기os,PIL,glob

//   PIL,os,glob
from PIL import Image
import os,glob
출력 디 렉 터 리 만 들 기

//        
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' #        .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') 
  #                   
실행 결과
pgm,png 를 막론하고 그들 은 모두.jpg 형식 으로 전환 하여 out 에 저장 합 니 다.path 폴 더 아래
在这里插入图片描述
在这里插入图片描述
전체 코드

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

from PIL import Image
import os,glob

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
  count = 0
  for files in glob.glob(in_path+'/*'):
    filepath,filename=os.path.split(files)
    out_file = filename[0:9]+'.png' #        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') #                   
총결산
python 에서 빠 른 파일 형식 일괄 변환 을 실현 하 는 방법 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 python 파일 형식 일괄 변환 내용 은 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 지원 바 랍 니 다!

좋은 웹페이지 즐겨찾기