We need to put some bmp Turn the picture into jpg, Part of the data is used opencv, But it is inconvenient to install , It is estimated to use other methods .
import os.path
from PIL import Image
from io import BytesIO
def scanDir(dirPath: str)->list:
""" Scan directory """
fileList = []
for top, dirs, nondirs in os.walk(dirPath):
for item in nondirs:
if item.split('.)[-1] == 'bmp':
fileList.append(os.path.join(top, item))
return fileList
def bmp2jpg(filePath):
fileLst = scanDir(filePath)
for oneFile in fileLst:
bmp = Image.open(oneFile)
output_buffer = BytesIO()
bmp.save(output_buffer,format ='JPEG')
byte_data = output_buffer.getvalue()
a = os.path.splitext(oneFile)[0]
tmpUrl = f'{a}.jpg'
try:
with open(tmpUrl, 'wb') as f:
f.write(byte_data)
except Exception as e:
pass