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 파일 형식 일괄 변환 내용 은 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 지원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
로마 숫자를 정수로 또는 그 반대로 변환그 중 하나는 로마 숫자를 정수로 변환하는 함수를 만드는 것이었고 두 번째는 그 반대를 수행하는 함수를 만드는 것이었습니다. 문자만 포함합니다'I', 'V', 'X', 'L', 'C', 'D', 'M' ; 문자열이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.