python bmp轉jpg 且灰度圖轉彩色


今天在簡書,上看到了一個 bmp轉jpg的代碼,便記錄一下。

# coding:utf-8
import os
from PIL import Image

# bmp 轉換為jpg,灰度圖轉RGB
def bmpToJpg_grayToRGB(file_path):
   for fileName in os.listdir(file_path):
       print(fileName)
       newFileName = fileName[0:fileName.find(".bmp")]+".jpg"
       print(newFileName)
       im = Image.open(file_path+"\\"+fileName)
       rgb = im.convert('RGB')      #灰度轉RGB
       rgb.save(file_path+"\\"+newFileName)

# 刪除原來的位圖
def deleteImages(file_path, imageFormat):
   command = "del "+file_path+"\\*."+imageFormat
   os.system(command)

def main():
   file_path = "D:\\models-master\\research\\object_detection\\images"
   bmpToJpg_grayToRGB(file_path)
   deleteImages(file_path, "bmp")

if __name__ == '__main__':
   main()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM