欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • Sanic框架流式傳輸操作示例

    Sanic框架流式傳輸操作示例

    這篇文章主要介紹了Sanic框架流式傳輸操作,結(jié)合實(shí)例形式分析了Sanic通過(guò)流請(qǐng)求與響應(yīng)傳輸操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-07-07
  • pytorch中unsqueeze用法小結(jié)

    pytorch中unsqueeze用法小結(jié)

    unsqueeze()的作用是用來(lái)增加給定tensor的維度的,本文主要介紹了pytorch中unsqueeze用法小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • Python單元和文檔測(cè)試實(shí)例詳解

    Python單元和文檔測(cè)試實(shí)例詳解

    這篇文章主要介紹了Python單元和文檔測(cè)試,結(jié)合實(shí)例形式分析了Python單元測(cè)試模塊unittest及文檔測(cè)試模塊doctest相關(guān)使用技巧,需要的朋友可以參考下
    2019-04-04
  • Python遠(yuǎn)程開(kāi)發(fā)環(huán)境部署與調(diào)試過(guò)程圖解

    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-12
  • python暴力解壓rar加密文件過(guò)程詳解

    python暴力解壓rar加密文件過(guò)程詳解

    這篇文章主要介紹了python解壓rar加密文件過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python六大開(kāi)源框架對(duì)比

    Python六大開(kāi)源框架對(duì)比

    在這篇文章里,我們將為Python Web開(kāi)發(fā)者回顧基于Python的6大Web應(yīng)用框架。無(wú)論你是出于愛(ài)好還是需求,這六大框架都可能會(huì)成為你工作上不錯(cuò)的得力助手。
    2015-10-10
  • OpenCV實(shí)現(xiàn)圖像濾波之雙邊濾波

    OpenCV實(shí)現(xiàn)圖像濾波之雙邊濾波

    這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)圖像濾波之雙邊濾波,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 在DOS界面如何運(yùn)行python的py文件

    在DOS界面如何運(yùn)行python的py文件

    這篇文章主要介紹了在DOS界面如何運(yùn)行python的py文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 淺談python中的數(shù)字類型與處理工具

    淺談python中的數(shù)字類型與處理工具

    下面小編就為大家?guī)?lái)一篇淺談python中的數(shù)字類型與處理工具。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情

    Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情

    這篇文章主要介紹了Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-06-06

最新評(píng)論