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