Python如何將bmp格式的圖片批量轉(zhuǎn)成jpg
更新時(shí)間:2023年03月25日 09:49:59 作者:雪地(>^ω^<)
這篇文章主要介紹了Python如何將bmp格式的圖片批量轉(zhuǎn)成jpg問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
將bmp格式的圖片批量轉(zhuǎn)成jpg
# *_* coding : UTF-8 *_* # 開(kāi)發(fā)人員: csu·pan-_-|| # 開(kāi)發(fā)時(shí)間: 2020/11/21 12:40 # 文件名稱: bmp_to_jpg.py # 開(kāi)發(fā)工具: PyCharm # 功能描述: 將bmp格式的圖片批量轉(zhuǎn)成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圖像格式轉(zhuǎn)換(bmp、jpg、png)
bmp轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)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)) ?# 轉(zhuǎn)為png bmp2png(r"D:\數(shù)據(jù)集\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數(shù)據(jù)集\20221105-18")
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python遠(yuǎn)程開(kāi)發(fā)環(huán)境部署與調(diào)試過(guò)程圖解
這篇文章主要介紹了Python遠(yuǎn)程開(kāi)發(fā)環(huán)境部署與調(diào)試過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情
這篇文章主要介紹了Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06