Python에서 bmp 형식의 이미지를 jpg로 일괄 변환하는 방법

PHPz
풀어 주다: 2023-04-28 09:22:14
앞으로
2272명이 탐색했습니다.

将bmp格式的图片批量转成jpg

# *_* coding : UTF-8 *_*
# 开发人员: csu·pan-_-||
# 开发时间: 2020/11/21 12:40
# 文件名称: bmp_to_jpg.py
# 开发工具: PyCharm
# 功能描述: 将bmp格式的图片批量转成jpg

import os
import cv2

# 图片的路径
bmp_dir = r'E:\Projects\bmp'
jpg_dir = r'E:\Projects\jpg'

filelists = os.listdir(bmp_dir)

for i,file in enumerate(filelists):
    # 读图,-1为不改变图片格式,0为灰度图  
    img = cv2.imread(os.path.join(bmp_dir,file),-1)
    newName = file.replace('.bmp','.jpg')
    cv2.imwrite(os.path.join(jpg_dir,newName),img)
    print('第%d张图:%s'%(i+1,newName))
로그인 후 복사

python图像格式转换(bmp、jpg、png)

bmp转png

import os
from PIL import Image
json_dir = r"D:\BMP2PNG"
label_names = os.listdir(json_dir)
label_dir = []
for filename in label_names:
    label_dir.append(os.path.join(json_dir,filename))

for i,filename in enumerate(label_dir):

    im = Image.open(filename)  # open ppm file

    newname = label_names[i].split('.')[0] + '.png'  # new name for png file
    im.save(os.path.join(json_dir,newname))
로그인 후 복사

bmp转jpg

import os
from PIL import Image
json_dir = r"D:\BMP2JPG"
label_names = os.listdir(json_dir)
label_dir = []
for filename in label_names:
    label_dir.append(os.path.join(json_dir,filename))

for i,filename in enumerate(label_dir):

    im = Image.open(filename)  # open ppm file

    newname = label_names[i].split('.')[0] + '.jpg'  # new name for png file
    im.save(os.path.join(json_dir,newname))
로그인 후 복사

遍历文件夹下包含子文件夹中的所有bmp转png

import os
from PIL import Image
import tqdm

def bmp2png(file_dir):
    for root, dirs, files in os.walk(file_dir):  # 获取所有文件
        # for file in files:  # 遍历所有文件名
        for idx,file in enumerate(tqdm.tqdm(files)):
            if os.path.splitext(file)[1] == '.bmp':   # 指定尾缀  ***重要***
                im = Image.open(os.path.join(root, file))  # open img file
                newname = file.split('.')[0] + '.png'  # new name for png file
                im.save(os.path.join(root, newname))  # 转为png

bmp2png(r"D:\数据集\20221105-18")
로그인 후 복사

遍历文件夹下包含子文件夹中的所有bmp重命名为png

import os
from PIL import Image
import tqdm

def bmp2png(file_dir):
    for root, dirs, files in os.walk(file_dir):  # 获取所有文件
        # for file in files:  # 遍历所有文件名
        for idx,file in enumerate(tqdm.tqdm(files)):
            if os.path.splitext(file)[1] == '.bmp':   # 指定尾缀  ***重要***
                newname = file.split('.')[0] + '.png'  # new name for png file
                if(os.path.exists(os.path.join(root, newname))):
                    os.remove(os.path.join(root, newname))
                os.rename(os.path.join(root, file),os.path.join(root, newname))
                print(os.path.join(root, file))

bmp2png(r"D:\PUCP数据集\20221105-18")
로그인 후 복사

위 내용은 Python에서 bmp 형식의 이미지를 jpg로 일괄 변환하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!