python bmp converts to jpg and deletes the original image

  • 2021-01-03 20:56:41
  • OfStack

As shown below:


# coding:utf-8
import os

from PIL import Image


# bmp  convert jpg
def bmpToJpg(file_path):
 for fileName in os.listdir(file_path):
  # print(fileName)
  newFileName = fileName[0:fileName.find("_")]+".jpg"
  print(newFileName)
  im = Image.open(file_path+"\\"+fileName)
  im.save(file_path+"\\"+newFileName)


#  Delete the original bitmap 
def deleteImages(file_path, imageFormat):
 command = "del "+file_path+"\\*."+imageFormat
 os.system(command)


def main():
 file_path = "D:\\VideoPhotos"
 bmpToJpg(file_path)
 deleteImages(file_path, "bmp")


if __name__ == '__main__':
 main()

Related articles: